mike1950r Posted June 4, 2023 Share Posted June 4, 2023 Hello, I have a problem handling @HotKeyPressed. I need an idea how to handle keystrokes in Switch @HotKeyPressed procedure as ONE keystroke no matter how long the user presses this key down. I have tried: While _IsPressed("xx", $hDLL) Sleep(250) WEnd But this did not solve the problem. If I press a key down constantly and release the key then, I get multiple keystrokes from this key some seconds long. May be someone had an idea how I can get only ONE keystroke then. Thanks in advance. Cheers mike Link to comment Share on other sites More sharing options...
Andreik Posted June 4, 2023 Share Posted June 4, 2023 Something like this: #include <Misc.au3> While True If _IsPressed('41') Then ConsoleWrite('Key [a] is pressed' & @CRLF) Do Sleep(10) Until Not _IsPressed('41') ConsoleWrite('Key [a] is released' & @CRLF) EndIf If _IsPressed('1b') Then Exit Sleep(10) WEnd When the words fail... music speaks. Link to comment Share on other sites More sharing options...
pixelsearch Posted June 4, 2023 Share Posted June 4, 2023 Mike, to avoid that, this is what I use : a function Do_Nothing() ... that does nothing #include <MsgBoxConstants.au3> #include <Misc.au3> ; Press Esc to terminate script, F5 to test "non-repeating" F5 key HotKeySet("{ESC}", "HotKeyPressed") HotKeySet("{F5}", "HotKeyPressed") Local $iInc = 0 While 1 Sleep(10) WEnd Func HotKeyPressed() Switch @HotKeyPressed ; the last hotkey pressed. Case "{ESC}" Exit Case "{F5}" HotKeySet("{F5}", "Do_Nothing") While _IsPressed("74") ; F5 key Sleep(10) WEnd $iInc += 1 ConsoleWrite("$iInc = " & $iInc & @crlf) HotKeySet("{F5}", "HotKeyPressed") EndSwitch EndFunc ;==>HotKeyPressed Func Do_Nothing() EndFunc Link to comment Share on other sites More sharing options...
pixelsearch Posted June 4, 2023 Share Posted June 4, 2023 Andreik: when I run your script, console output is ok, but what about all others "aaaaa" that appear in Scite while the "a" key is pressed ? If I change F5 with "a" (as a hotkey) in my script, no "aaaaaa" appear in Scite (just tested) Link to comment Share on other sites More sharing options...
Andreik Posted June 5, 2023 Share Posted June 5, 2023 (edited) @pixelsearch It' not very clear what OP ask for or I am terrible in english. Give him a chance to decide whether he wants to suppress the send of the key or just the logging. BTW, you don't really need a dummy function. #include <MsgBoxConstants.au3> #include <Misc.au3> ; Press Esc to terminate script, F5 to test "non-repeating" F5 key HotKeySet("{ESC}", "HotKeyPressed") HotKeySet("{F5}", "HotKeyPressed") While 1 Sleep(10) WEnd Func HotKeyPressed() Switch @HotKeyPressed ; the last hotkey pressed. Case "{ESC}" Exit Case "{F5}" Static $iInc = 0 HotKeySet("{F5}") While _IsPressed("74") ; F5 key Sleep(10) WEnd $iInc += 1 ConsoleWrite("$iInc = " & $iInc & @crlf) HotKeySet("{F5}", "HotKeyPressed") EndSwitch EndFunc ;==>HotKeyPressed Edited June 5, 2023 by Andreik When the words fail... music speaks. Link to comment Share on other sites More sharing options...
pixelsearch Posted June 5, 2023 Share Posted June 5, 2023 @andreik without the dummy function, you will find serious issues (try F1 or F3 or "a" as hotkeyset, instead of F5) As F5 is inactive while a script is executed in Scite, that's why it appears that you can get rid of the dummy function with the F5 test, but if you assign hotkey to keys that can be active in the Scite environment (F3, F1, "a" etc...) then you will see the difference. HotKeySet("{F5}", "Do_Nothing" has to be called as soon as possible (e.g 1st code line) after Case "{F5}" . If there are one or several lines before it, then issues may appear too. If I understood correctly OP, he simply wants that a long key press doesn't generate dozen of buffered key presses, e.g. a long key press needs to be assimilated to a single key press, without further display (or loop) consequences. As you said, let's wait OP to see if what we suggested is ok with him Musashi 1 Link to comment Share on other sites More sharing options...
Andreik Posted June 5, 2023 Share Posted June 5, 2023 I see what you mean with serious issues. Quote As you said, let's wait OP to see if what we suggested is ok with him I am curious as well. When the words fail... music speaks. Link to comment Share on other sites More sharing options...
mike1950r Posted June 5, 2023 Author Share Posted June 5, 2023 Hello Andreik and Pixelsearch, thanks for your proposals. Unfortunately none of them work for me. If I hold the key constantly down only one action is done, like i want, but as soon as i leave the key going up, one action after the other is done for several seconds, depending how long i hold it down before. So the problem seems to be lifting the key. Have to search for another solution. It should work like a knitter-switch. It switches only ONCE no matter how long you press the switch down. For switching TWICE it must be lifted before. Thanks anyway. Cheers mike Link to comment Share on other sites More sharing options...
mike1950r Posted June 5, 2023 Author Share Posted June 5, 2023 My problem is: Case "{F2}" _IsPressed("71") Result of _IsPressed is False. Cause this would be a solution, that my action is only done while key is pressed and not when key is released. Have to check if this only happens with certain keys. See you. Cheers mike Link to comment Share on other sites More sharing options...
mike1950r Posted June 5, 2023 Author Share Posted June 5, 2023 As I feared: _Ispressed does not work after Case of @HotKeyPressed here. Very strange. Cheers mike Link to comment Share on other sites More sharing options...
pixelsearch Posted June 5, 2023 Share Posted June 5, 2023 Mike, in my script above, " _Ispressed works after Case of @HotKeyPressed " As it doesn't seem to work for you, what about showing some runnable code concerning your issue, maybe a short reproducer ? Link to comment Share on other sites More sharing options...
Danp2 Posted June 5, 2023 Share Posted June 5, 2023 (edited) @pixelsearch Sometimes I get two instances of the Case "{F5}" being triggered when I run your code. It's like there's an F5 left in the keyboard buffer when the key is released. Edit: Adding a Sleep(10) after the lone ConsoleWrite appears to resolve this for me. Edited June 5, 2023 by Danp2 Latest Webdriver UDF Release Webdriver Wiki FAQs Link to comment Share on other sites More sharing options...
Andreik Posted June 5, 2023 Share Posted June 5, 2023 @Danp2 I saw that too but can be fixed. There might be another solution using keyboard hooks but I can't play this game, it's more like guess what I need. When the words fail... music speaks. Link to comment Share on other sites More sharing options...
pixelsearch Posted June 5, 2023 Share Posted June 5, 2023 @Danp2 the "sometimes" part makes it hard to solve I can't reproduce it (God knows I tried), maybe it's the computer speed that makes the difference (mine is slow) Dan, you could try to place the Case F5 before Case Esc, so it's triggered quicker, or While _IsPressed("74") using a 2nd parameter ($hDll) or even get rid of the While 10 inside the _IsPressed("74") loop (it won't be too harmful as we're not spending hours with our finger pressed on the key) . On my computer, as soon as there is a loop with _IsPressed("74"), then the key doesn't repeat itself, no matter if there is a Sleep(...) or not inside the loop. I wish I could reproduce it so I won't suggest "blind" tries. As @Andreik just said, a keyboard hook would be interesting. I got one scripted a few months ago, which indicated how many times the key was repeated (when you release it), but it would probably lead us to other questions. Link to comment Share on other sites More sharing options...
Solution mike1950r Posted June 5, 2023 Author Solution Share Posted June 5, 2023 (edited) OK I found, where the problem came from. Here is now the working function: expandcollapse popupFunc VideoPlayVLCHotKeyPressed() Local $sWindowTitle $sWindowTitle = WindowProcessTitle("vlc.exe") Switch @HotKeyPressed VideoPlayVLCHotKeySetUndo() WinActivate($sWindowTitle, "") Case "{F2}" If _IsPressed("71") Then Send("^{DOWN}") EndIf While _IsPressed("71") Sleep(10) WEnd Case "{F3}" If _IsPressed("72") Then Send("^{UP}") EndIf While _IsPressed("72") Sleep(10) WEnd Case "{F5}" If _IsPressed("74") Then Send("+{LEFT}") EndIf While _IsPressed("74") Sleep(10) WEnd Case "{F6}" If _IsPressed("75") Then Send("+{RIGHT}") EndIf While _IsPressed("75") Sleep(10) WEnd Case "{PAUSE}" Send("{SPACE}") Case "{END}" If NumLockState() = "ON" Then ProcessClose("vlc.exe") Else Send("{NUMLOCK}") EndIf EndSwitch VideoPlayVLCHotKeySet() EndFunc ;==>VideoPlayVLCHotKeyPressed In fact the problem was, that the WinActivate command was written before Switch @HotKeyPressed. This must be after Switch @HotKeyPressed. May be this window sends out key messages when beeing activated, I don't know. However after this changing the function works like it should. But the While loop after the toDo is not enough. It must also be the If _IsPressed before the toDo. Otherwise you get a lot of keystrokes coming in when you release the key. OH my ... 🙃 I think it would be a good modification of the Hotkey procedure to have a switch for treating keystrokes as single keystrokes if needed. Thanks to everybody. Cheers mike Edited June 5, 2023 by mike1950r Link to comment Share on other sites More sharing options...
mike1950r Posted June 5, 2023 Author Share Posted June 5, 2023 Just one more question: Does _IsPressed also work with any key like wildcard? This would make things much easier. Cheers mike Link to comment Share on other sites More sharing options...
Danp2 Posted June 5, 2023 Share Posted June 5, 2023 @pixelsearch I tried the suggested changes, but none of them fixed the issue for me. Changing the Sleep delay to 100 within the While loop helped to minimize the occurrences, but I was still able to trigger the behavior on occasion. I believe it is a timing issue due to the computer speed. Without the added Sleep before restoring the hotkey, an instance of the hotkey could be left unprocessed in the keyboard buffer. Latest Webdriver UDF Release Webdriver Wiki FAQs Link to comment Share on other sites More sharing options...
pixelsearch Posted June 5, 2023 Share Posted June 5, 2023 Danp2, thanks for the tests, they'll be useful to anyone facing the same situation. Glad OP could make it. Link to comment Share on other sites More sharing options...
mike1950r Posted June 5, 2023 Author Share Posted June 5, 2023 (edited) resume: Case "{F2}" If _IsPressed("71") Then Do your work ... EndIf While _IsPressed("71") Sleep(10) WEnd this works excellent Cheers mike Edited June 5, 2023 by mike1950r Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now