DAY 016: _MOUSEINPUT - Printable Version +- QB64 Phoenix Edition (https://qb64phoenix.com/forum) +-- Forum: Official Links (https://qb64phoenix.com/forum/forumdisplay.php?fid=16) +--- Forum: Learning Resources and Archives (https://qb64phoenix.com/forum/forumdisplay.php?fid=13) +---- Forum: Keyword of the Day! (https://qb64phoenix.com/forum/forumdisplay.php?fid=49) +---- Thread: DAY 016: _MOUSEINPUT (/showthread.php?tid=1165) |
RE: DAY 016: _MOUSEINPUT - SMcNeill - 11-22-2022 (11-22-2022, 12:38 AM)james2464 Wrote: I think my confusion centers around the "AND NOT". I have never used this and I need to give this a try in the future. Thanks! You're almost there with your own statement: "I believe it means literally that: if mb is true..." ^This part is correct. The next part should, however, be: "and if NOT oldmb is true..;" In other words, "if mb is true and oldmb is false". (Not true is false, after all.) RE: DAY 016: _MOUSEINPUT - Pete - 11-22-2022 I use mouse routines extensively. Good routines use variables to indicate a button is down or released, if a double click is initiated or if a drag event is occurring. Drag is simple enough with a left mouse button down variable and checking if the mouse has moved. I believe we have a _MOUSEMOVEMENT keyword, but I'm old school. I just go with an oldmy and oldmy variable in the loop. If the left mouse button is down and my <> oldmy or mx <> oldmx then the drag routine gets called. Fun stuff when down properly and a bit easier than it used to be with CALL INTERRUPT routines, although they still function in QB64. Note however that CALL ABSOLUTE mouse routines are not supported. Pete RE: DAY 016: _MOUSEINPUT - SMcNeill - 11-22-2022 @Pete If you're going that complex, use MBS: https://qb64phoenix.com/forum/showthread.php?tid=138 RE: DAY 016: _MOUSEINPUT - Pete - 11-22-2022 Nice, but my mouse routines have more BS than yours! Ah, wait a minute; that didn't come out right... Oh, and in other news, Apparently Joe Brandon gave your code a pardon. (Joe Biden pardons Saudi Leader.) Pete RE: DAY 016: _MOUSEINPUT - SMcNeill - 11-22-2022 We were also discussing this topic over at our Discord channel, and it seems there may be a few use cases where you'd actually place more code inside the While: Wend loop than I mentioned -- and that's basically whenever your main loop is running at an incredibly slow rate. For example: Code: (Select All) oldmb = -1 'count as if the mouse is down to begin with Try the above and see how many times you have to click the mouse before a click registers. (For me, it's about half a dozen times, give or take.) Your processing time is much less than your down time with the limit, and a vast majority of mouse buttons are lost when you clear the buffer. This isn't exactly an idea approach to handling things... So, we can try something like the following: Code: (Select All) oldmb = -1 Now here, we can click all we want, and every click event is read. The problem with this approach is that our program is running one loop per second, the user is entering multiple clicks per second -- we're going to generate lag in there, no matter what!! If I can count one penny per second, and you suddenly hand me a dozen pennies, it's going to take me 12 seconds before I can do anything else besides count those pennies. < -- That's basically what we're seeing here. So, we can take a middle ground approach: Code: (Select All) oldmb = -1 Like this, we check for a mouseclick inside our mouseinput loop, and we basically set a flag if one occurs. It'll limit us to ONE click event per loop, no matter how many times the user mashes in the button, so we'll end up missing some clicks -- but we won't lag up the rest of our program either. We're running at one loop per second, and we're throttling clicks down to a maximum of one click per second. For most people, and most programs (which you seldom ever see with less than a limit 30, 60, or even higher), this type of concern isn't one they'd ever have to worry over. Their program polls the mouse faster than a human can click a button up and down, so there's no chance for that backlog to appear in the buffer to the point where they start to miss events. Once a program gets bogged down to the point where mouseclicks CAN be missed, however, there's really no "golden" solution for how to deal with things. Your three choices are basically: 1) Skip events so the program flows as smoothly as possible. 2) Deal with every event, even if it causes the program to have to pause execution until it's caught up and cleared the buffer. 3) Try to find a middle ground and watch to see if an event occured during the previous execution of the main program, and if so, deal with it -- even if you lose some other events in the process. I mentioned that you never want anything inside that While - Wend _MOUSEINPUT Loop... That's not entirely true in extreme use cases, like the ones I showcased above. One thing to remember: For every rule someone gives you in programming, there's always some exception to that rule -- no matter how rare, extreme, or unique it might be. RE: DAY 016: _MOUSEINPUT - Pete - 11-22-2022 Ever notice that some apps trigger an event when the left mouse is pressed, but a few trigger it only after the left mouse button once pressed is released? The following demonstrates how this can be accomplished... Code: (Select All) REM click_event = 1 to initiate on button press. Pete RE: DAY 016: _MOUSEINPUT - mnrvovrfc - 12-05-2022 The other problem would be like a program I wrote which read the right mouse button click to quit. The program ended with "SYSTEM", that is, without "Press any key to continue" prompt, and then suddenly a context menu was opened in File Explorer or another app that happened to have a window just under that of my program. It was annoying but I had trouble killing the mouse events upon user program finish. RE: DAY 016: _MOUSEINPUT - Pete - 12-05-2022 Which leads into yet another discussion, how to save yourself from an unintended exit. So to demonstrate how this is done, try the following... If you press the left mouse button down, over the exit icon, but then move the cursor off it before you release the button, you avoid the system action with this mini-mouse routine! Code: (Select All) DIM mse AS mm ' Let's do a TYPE instead of passing all variables or doing a DIM SHARE. Pete RE: DAY 016: _MOUSEINPUT - SMcNeill - 12-05-2022 @Pete Or you just code a confirmation box into your program. "Are you certain you want to exit now? Unsaved data may be lost." Or, if they've already saved: "Thank you for using a Steve-smart program. Please try all our other fine products at https://smcneill.online Mention "Steve is Awesome" and get 50% off all product prices!" RE: DAY 016: _MOUSEINPUT - Pete - 12-05-2022 Let's not get ahead of ourselves there Sparky. We want to save that for the new keyword or until we get to _EXIT. That's what I've used for all my previous files programs, although I auto-update most stuff. LOL @ Channeling Mike Lindell. I do that too sometimes. Hey, I just had a thought... I guess that makes you The Farmer in Lindell. Oh no... H.R. memo again!!!! Pete |