Posts: 48
Threads: 18
Joined: May 2011
Is there a way to determine how long a keyboard key (or keyboard sequence such as shift-alt-p) has been pressed? I would like to kick off a function based on how long the key (or key sequence) is pressed. For example, I could detect morse code with a particular key (short press = dot, long press=dash).
Posts: 12,087
Threads: 142
Joined: Dec 2002
Function
key_pressed_time
function# nCode message KBDLLHOOKSTRUCT&k
;This code runs when you launch this function. It starts or stops detecting key times.
if(getopt(nargs)=0)
,if(getopt(nthreads)>1) shutdown -6 0 "key_pressed_time"; ret
,AddTrayIcon "$qm$\keyboard.ico" "QM - key pressed time"
,int hHook=SetWindowsHookEx(WH_KEYBOARD_LL &key_pressed_time _hinst 0)
,opt waitmsg 1
,wait -1
,UnhookWindowsHookEx hHook
,ret
;This code runs on a key event.
if(nCode<0) goto gNext
if(k.flags&LLKHF_INJECTED) goto gNext
FormatKeyString k.vkCode 0 &_s; out "%s %s" _s iif(k.flags&LLKHF_UP "up" "down") ;;debug
ARRAY(int)+ g_key_times; if(!g_key_times.len) g_key_times.create(256)
int vk=k.vkCode&0xff
if(k.flags&LLKHF_UP=0)
,if(g_key_times[vk]) goto gNext ;;repeated
,g_key_times[vk]=iif(k.time k.time -1)
,goto gNext
int t=k.time-g_key_times[vk]
g_key_times[vk]=0
;____________________________________________
;This code runs on a user-pressed key up event.
out t ;;key pressed duration in milliseconds
;add more code here, for example:
;MorseFunction vk t
;____________________________________________
;gNext
ret CallNextHookEx(0 nCode message &k)
Posts: 48
Threads: 18
Joined: May 2011
Could you give an example of how to call this function?
Posts: 12,087
Threads: 142
Joined: Dec 2002
Create function key_pressed_time and paste the code there.
Click Run button. Or assign a trigger.
While it is running, press a key to see how it works.
Click Run again when want to stop.
Replace out t with your code.
Finally disable the FormatKeyString line.
Posts: 48
Threads: 18
Joined: May 2011
Got it. Thanks Gintaras! Great support!