Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Long press trigger
#1
Is it possible to set a long press key as a trigger? For example, I would like a macro to be executed only if I keep the "a" key pressed for one second or more.
#2
Technically possible, but not recommended for letter keys.
Detecting a long press requires delaying the key event (type "a" on key-up), which would make normal typing feel laggy and unreliable. This kind of behavior can also break other applications. Is it OK?
#3
Thank you for your answer.

What I am looking for is a quick, simple and customizable way to type foreign characters. I have tried several systems, including Popchar and Extended keboards, and now I think that the most logical and quick system is the one used on smartphones: you keep a key touched and a popup lets you choose from additional letters or symbols.

An imitation of this criterion on Windows is keyxpat (https://keyxpat.com), where, if you keep a key pressed, a sort of metronome starts, and a different letter or symbol will be typed depending how long you keep the letter pressed. I think that the principle of keeping a letter pressed is good, what I don’t like is the metronome criterion, because this way you need to remember what symbol corresponds to different durations. Instead, it should work like Android, a panel should popup and then you choose, by using arrow keys or other keys, even better if you could just use the same key that you kept pressed. The criterion of the popup panel is used on “Quick Accent”, that is part of a series of Powertoys for Windows, but the trigger of this program is based on combination of keys, not on keeping pressed just one key.

At the moment the quickest way I am using is based on Quick Macros: I press F6 and this button triggers at the same time a keyboard remapping for some keys and a popup picture that shows my keyboard and the new letters that will be typed with each key. Once I press a key, Quick Macros closes the popup picture and stops the key remapping. What I don’t like in this system is that you have to press F6. The advantage is that you don’t have to do any additional action, like moving with the mouse cursor through different symbols, to choose what you need to type.

I understand that the system of long press could break other applications and it could be itself not so reliable. I would like to test something with Quick Macros, but I would need a starting point how Quick Macros would detect a long press, may be also customizing a minimum time of keeping the key pressed, to make the trigger start.
#4
A starting point.

Function Long_press_key_trigger
Code:
Copy      Help
int hh=SetWindowsHookEx(WH_KEYBOARD_LL &sub.Hook _hinst 0)
opt waitmsg 1
wait -1
UnhookWindowsHookEx hh


#sub Hook
function# nCode message KBDLLHOOKSTRUCT&k
if(nCode<0) goto gNext
if(k.flags&LLKHF_INJECTED) goto gNext

;FormatKeyString k.vkCode 0 &_s; out "%s %s%s" _s iif(k.flags&LLKHF_UP "up" "down") iif(k.flags&LLKHF_INJECTED ", injected" "") ;;debug

sel k.vkCode
,case ['A','E','I','O','U'] ;;TODO: edit if need. Note: these are keys (US or similar keyboard), not characters.
,case else goto gNext

int-- t_key t_time t_triggerOnce

if !(k.flags&LLKHF_UP) ;;key down
,if k.vkCode!=t_key or k.time-t_time>10000
,,t_key=k.vkCode
,,t_time=k.time
,,t_triggerOnce=0
,,;out "key down: %c" k.vkCode
,else
,,if !t_triggerOnce and k.time-t_time>=950 ;;TODO: edit the trigger time 950 ms if need
,,,t_triggerOnce=1
,,,mac "sub.Triggered" "" k.vkCode
,,,;note: this code uses the auto-repeat feature. Will not work if it is turned off.
,,,
,,ret 1 ;;prevent auto-repeat
else ;;key up
,t_key=0

;gNext
ret CallNextHookEx(0 nCode message &k)


#sub Triggered
function vk

key B ;;Backspace erases the character

out "trigger for key: %c" vk

;TODO: add your code here

;example: replace 'a' with 'ą'
int upper=ifk(S)^ifk(K 1)
sel vk
,case 'A'
,if(upper) key "Ą"; else key "ą"
,
,case 'E'
,;...
#5
Thank you very much, it works, but I am not sure I am using it the best way.

As a first thing, I just created the function and put your code inside, but this way it does nothing: if I keep "A" pressed, it just starts repeating "a" the normal way how it is supposed to work in Windows.

Then I set the function as triggered by the key "A"; to avoid it to interfere with other programs or macros, I had to untick the "Eat" square. This way it works: simply hitting the key "A" types "a", keeping it pressed executes what I put at the end of the fuction. In my case I have made it show a menu of foreign letters.

But this means that I need to make different copies of the function for each key I want to work this way. Is this the way how it is supposed to work?

What makes me unsure is the fact that the code of the function seems designed to work for many keys with just one copy of the function, because the code has

case ['A','E','I','O','U']

which at the end is recalled by

,case 'A'
,if(upper) key "Ą"; else key "ą"
,
,case 'E'
,;...

But in this case I cannot understand how the function is supposed to be triggered, because, as I said, if I don’t set a trigger for the function, it seems to me that it doesn’t do anything.

Sorry about my zero experience at coding and understanding code: I am not a programmer, I just try to do my best. Thank you for your patience.
#6
Run the function. It itself creates multiple triggers, for each key specified in the case [...]. It runs all the time.

To run at startup, use trigger QM > QM file loaded. Don't check "synchronously".

Make sure to not run multiple instances simultaneously. There is a setting in Properties to prevent it.
#7
Wow, that’s fantastic, simple and powerful!
Thank you!


Forum Jump:


Users browsing this thread: 1 Guest(s)