Jump to content
Sign in to follow this  
Bori20

Action while a key is pressed?

Recommended Posts

Bori20

Hi all,

I'm trying to create a script that will show a window WHILE a given key is pressed. For example, while the 'tab' is pressed, or while 'Caps lock' is pressed. (Similar to Enso Launcher, if you know). The window should disappear when the key is released.

I tried using '_isPressed', but it does not work as i expected. See the sample script

#include <Misc.au3>

ConsoleWrite("Starting!" & @CRLF)
$ok = HotKeySet("{TAB}", "buttonPressed");
While 1
    Sleep(10000)
WEnd

Func buttonPressed()
    ConsoleWrite("Entering buttonPressed" & @CRLF)
    $dll = DllOpen("user32.dll")
    While _IsPressed(9, $dll)
        Sleep(10)
        ConsoleWrite("   Still pressed..." & @CRLF)
    WEnd
    DllClose($dll)
    ConsoleWrite("Leaving buttonPressed" & @CRLF)
    
EndFunc   ;==>buttonPressed

I thought that for each TAB click it will say

Entering buttonPressed
Still pressed...
Still pressed...
Still pressed...
Still pressed...
Leaving buttonPressed

But it seems that the function gets activated many times although I am still holding the key. Maybe it's the auto-repeat? it also happens with 'caps' as the key, so I'm not sure it it is the auto-repeat.

is there another way to do this? or am I missing something?

thanks

Bori

Share this post


Link to post
Share on other sites
Tvern

...it seems that the function gets activated many times although I am still holding the key.

Sounds like you need to unset the hotkey when you enter the function. Try adding:

HotKeySet("{TAB}") before the while loop in the function and

HotKeySet("{TAB}", "buttonPressed") after the while loop in the function.

Share this post


Link to post
Share on other sites
Bori20

Sounds like you need to unset the hotkey when you enter the function. Try adding:

HotKeySet("{TAB}") before the while loop in the function and

HotKeySet("{TAB}", "buttonPressed") after the while loop in the function.

Thanks, Tvern, it improves the behaviour a lot.

However, it's still not correct.

What I get now is the output I wanted, but the hotkey still goes to the other application

it's a bit hard to explain, but easy to understand if you run it.

Here's the code:

#include <Misc.au3>
ConsoleWrite("Starting!" & @CRLF)
$ok = HotKeySet("{TAB}", "buttonPressed");
While 1
    Sleep(10000)
WEnd

Func buttonPressed()
    HotKeySet("{TAB}");
    ConsoleWrite("Entering buttonPressed" & @CRLF)
    $dll = DllOpen("user32.dll")
    While _IsPressed(9, $dll)
        Sleep(100)
        ConsoleWrite("   Still pressed.  looping "&@MIN &@SEC & @CRLF)
    WEnd
    DllClose($dll)
    HotKeySet("{TAB}", "buttonPressed");
    ConsoleWrite("Leaving buttonPressed" & @CRLF)
    
EndFunc   ;==>buttonPressed

when it runs, it prints "Still looping" while i hold the TAB key, but the SCITE editor I have behind still receives the tabs, although buttonPressed() has not finished. Can I consume the tab event in my code, to make it not go to the application currently focused?

Thanks

Share this post


Link to post
Share on other sites
Tvern

Quick fix:

Instead of unsetting the hotkey at the start of the function, set it to an empty function that does nothing.

More involved approach:

Capture the windows message WM_KEYDOWN and write a function to handle tab down. (Perhaps there is a message WM_KEYPRESSED or something aswell. I'm not sure)

Share this post


Link to post
Share on other sites
Bori20

Cool. I used the "WM_KEYDOWN" method you suggested, and it seems to work just fine.

Thanks !

Share this post


Link to post
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
Sign in to follow this  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.