Jump to content

@HotKeyPressed Problem


Go to solution Solved by mike1950r,

Recommended Posts

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

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

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

@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 by Andreik

When the words fail... music speaks.

Link to comment
Share on other sites

@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 :)
Link to comment
Share on other sites

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

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

@Danp2 the "sometimes" part makes it hard to solve :D
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

  • Solution

OK I found, where the problem came from.

Here is now the working function:

Func 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 by mike1950r
Link to comment
Share on other sites

@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.

Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...