Jump to content
Sign in to follow this  

Hotkey for key pressed

Recommended Posts



I'm having a problem:

I need a function to run while a key is pressed.

Basically, I tried it with two functions (start and stop) that are bound by HotKeySet to {x down} and {x up}.

But the second Hotkeyset seems to overwrite the first and always match for keypress and release of key x.

Quite unexpected for me, since x down/up works for Send().

I'm sure, there is a way to achieve what I tried, but unfortunately I couldn't find a solution.

I'd be very happy if someone could help me there.

Thanks :mellow:

Share this post

Link to post
Share on other sites


Welcome to the AutoIt forum. :mellow:

It is not surprising at all. Send uses the codes which keys produce as they are pressed and released (different codes for the 2 actions). HoKeySet only fires the associated function as the specified key is operated - there is no separate function for the release.

You need to use _IsPressed to detect the key being pressed. Perhaps something like this:

#include <GUIConstantsEx.au3>
#include <Misc.au3>

$hGUI = GUICreate("Test", 500, 500)

$hLabel = GUICtrlCreateLabel("Not running", 10, 10, 150, 20)

$hCounter = GUICtrlCreateLabel("", 10, 50, 30, 30)


$dll = DllOpen("user32.dll")

While 1

    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE

    If _IsPressed("58", $dll) Then _RunFunction()


Func _RunFunction()

    Local $iCount = 0

    GUICtrlSetData($hLabel, "Running")

        $iCount += 1
        GUICtrlSetData($hCounter, $iCount)

        Sleep(100) ; Idle a bit so you can see the counter increasing

    Until Not _IsPressed("58", $dll)

    GUICtrlSetData($hLabel, "Not running")
    GUICtrlSetData($hCounter, "")


You do need to keep checking the _IsPressed function to detect the key release in your function - but as it usually contains a loop this is not too much of a problem.

I hope that helps. Ask if anything is unclear. :(


Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:


ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area


Share this post

Link to post
Share on other sites

thank you very much.

looks fine to me. will give it a try.

works fine :mellow:

Edited by frankk

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