Jump to content

Recommended Posts

Posted

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

Posted
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

"I think you are searching a bug where there is no bug... don't listen to bad advice."

Posted
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)

"I think you are searching a bug where there is no bug... don't listen to bad advice."

Posted (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 by Andreik
Posted
@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 :)

"I think you are searching a bug where there is no bug... don't listen to bad advice."

Posted

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

Posted

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

Posted
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 ?

"I think you are searching a bug where there is no bug... don't listen to bad advice."

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

"I think you are searching a bug where there is no bug... don't listen to bad advice."

  • Solution
Posted (edited)

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
Posted

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

Posted
Danp2, thanks for the tests, they'll be useful to anyone facing the same situation.
Glad OP could make it.

"I think you are searching a bug where there is no bug... don't listen to bad advice."

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
  • Recently Browsing   0 members

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