Capture keypress in a function

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
Relayer
Posts: 160
Joined: 30 Sep 2013, 13:09
Location: Delaware, USA

Capture keypress in a function

01 Oct 2014, 10:17

Hi,

I have a mental block that needs a kick. I have a function that contains a loop terminated by a mouse click:

suspend, on
myFunction()
{
While, !(GetKeyState("LButton", "p"))
{
<do stuff that takes time>
}
}
suspend, off

The LButton is used as a hotkey for other things that are running at the same time, hence the use of suspend. The problem with this approach is that the mouse click is sampled once per loop which allows it to be missed. The user must click multiple times or hold it down for it to be recognized. Looking for it inside the loop just slows things down. Since I'm in a function it is not convenient to use the hotkey command with an associated label.

What's the best way to handle such a situation?

Relayer
Ronins
Posts: 85
Joined: 02 Oct 2013, 11:38

Re: Capture keypress in a function

01 Oct 2014, 12:24

you could use a global variable with a hotkey. Hotkey simply sets it to 0 and when the loop starts all over, the variable is checked. Since the variable is already at 0 now, the loop exits. It goes somewhere along the lines of

Code: Select all

Gvar := 1

LButton::
GVar := 0
return

MyFunction()
{
    global GVar
    while(GVar)
        stuffs
}
Try out CMD class
User avatar
Relayer
Posts: 160
Joined: 30 Sep 2013, 13:09
Location: Delaware, USA

Re: Capture keypress in a function

01 Oct 2014, 13:30

Thank you Ronins!

I had considered that but my function is part of a class library. I want to do this in a way that the host script doesn't need to know anything about what the class function is doing. Your solution requires the host script to set the global variable as part of the hotkey routine.

There may not be an easy way to do this.

Relayer
lexikos
Posts: 9690
Joined: 30 Sep 2013, 04:07
Contact:

Re: Capture keypress in a function

01 Oct 2014, 17:34

You can use the Hotkey command to dynamically register a ~LButton hotkey. To avoid modifying some other LButton hotkey, you can use #if/#ifwin with a condition which is unique but always true.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: LAPIII, peter_ahk and 296 guests