KeyWait for multiple keys ?? Topic is solved

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
User avatar
yfjuu6
Posts: 125
Joined: 28 Apr 2023, 15:28

KeyWait for multiple keys ??

04 Mar 2024, 17:11

I got this function from here : viewtopic.php?f=82&t=126256
It waits for one or multiple keys to be pressed :

Code: Select all

    Key_Wait_Multi(keys, options := "") {
    ih := InputHook("L1 " options)
    ih.KeyOpt(keys, "E")
    ih.Start()
    ih.Wait()
    return (ih.EndReason = "EndKey")
}

if Key_Wait_Multi("a{SC34}{F12}","T2") {
      MsgBox("you pressed on of the listed keys")
}
while waiting, I want when pressing any other key different from the listed keys e.g "c" I want it to be sent (written) from the first press no from the second press.
Because now, while waiting, when I press non-listed key e.g "c" nothing happens from the first press But when I press It again (second press) It send "c".

Any help to do so Please!
User avatar
mikeyww
Posts: 27145
Joined: 09 Sep 2014, 18:38

Re: KeyWait for multiple keys ??

04 Mar 2024, 20:25

Code: Select all

#Requires AutoHotkey v2.0
ih := InputHook('I', 'a{SC34}{F12}')
ih.OnChar := ih_Char
ih.Start(), ih.Wait()
MsgBox 'Done!', 'Status', 'Iconi'

ih_Char(ih, char) {
 Static last := ''
 If char != last
  SendText char
 last := char
}
User avatar
yfjuu6
Posts: 125
Joined: 28 Apr 2023, 15:28

Re: KeyWait for multiple keys ??

05 Mar 2024, 10:37

mikeyww wrote:
04 Mar 2024, 20:25

Code: Select all

#Requires AutoHotkey v2.0
ih := InputHook('I', 'a{SC34}{F12}')
ih.OnChar := ih_Char
ih.Start(), ih.Wait()
MsgBox 'Done!', 'Status', 'Iconi'

ih_Char(ih, char) {
 Static last := ''
 If char != last
  SendText char
 last := char
}
I don't know how to use It.
I want exactly what this function does :

Code: Select all

Wait_one_Key(key, options := "") { ; one key
    key_pressed := false
    ih := InputHook(options)
    ; ih.KeyOpt("{All}", "+INS") ; make all keys ignored and visible
    ih.KeyOpt("{All}", "N")
    ih.MinSendLevel := 1
    ih.OnKeyDown := (ih, vk, sc) => (
        key_pressed := GetKeyName(Format("vk{:x}sc{:x}", VK, SC)),
        sc := Format("sc{:x}", sc),
        key_pressed := (key = key_pressed) or (key = sc),
        (!key_pressed ? Send('{' sc '}') : 0),
        ih.Stop()
    )
    ih.Start(), ih.Wait() ; start input hook ; wait for input hook to end
    return key_pressed
}
The problems in this function are :
1- It Doesn't wait for multiple keys
2- Doesn't accept scan code (SC) or Virtual keycodes (VK).

So the first function works well and I want It to work like this function :
while waiting, I want when pressing any other key different from the listed keys e.g "c" I want it to be sent (written) from the first press no from the second press.
Because now, while waiting, when I press non-listed key e.g "c" nothing happens from the first press But when I press It again (second press) It send "c".
User avatar
mikeyww
Posts: 27145
Joined: 09 Sep 2014, 18:38

Re: KeyWait for multiple keys ??

05 Mar 2024, 10:55

From what I can tell, my script matches your description. Perhaps others will have a better idea of what you need.
ntepa
Posts: 432
Joined: 19 Oct 2022, 20:52

Re: KeyWait for multiple keys ??

05 Mar 2024, 14:14

Use the V option to allow keys to be sent when InputHook is running. I also added option S to prevent sending the listed keys:

Code: Select all

Key_Wait_Multi(keys, options := "") {
    ih := InputHook("L1 " options)
    ih.KeyOpt(keys, "ES")
    ih.Start()
    ih.Wait()
    return (ih.EndReason = "EndKey")
}

if Key_Wait_Multi("a{SC34}{F12}","T2 V") {
      MsgBox("you pressed on of the listed keys")
}
User avatar
yfjuu6
Posts: 125
Joined: 28 Apr 2023, 15:28

Re: KeyWait for multiple keys ??

05 Mar 2024, 15:53

ntepa wrote:
05 Mar 2024, 14:14
Use the V option to allow keys to be sent when InputHook is running. I also added option S to prevent sending the listed keys:

Code: Select all

Key_Wait_Multi(keys, options := "") {
    ih := InputHook("L1 " options)
    ih.KeyOpt(keys, "ES")
    ih.Start()
    ih.Wait()
    return (ih.EndReason = "EndKey")
}

if Key_Wait_Multi("a{SC34}{F12}","T2 V") {
      MsgBox("you pressed on of the listed keys")
}
It works.
How to always (By default) allow keys to be sent when InputHook is running without adding "V" as an option, just this :
if Key_Wait_Mult("a{SC34}{F11}")
instead of :
if Key_Wait_Multi("a{SC34}{F12}","T2 V")
I tried this But not work:
ih.KeyOpt(keys, "V")
ntepa
Posts: 432
Joined: 19 Oct 2022, 20:52

Re: KeyWait for multiple keys ??

05 Mar 2024, 16:22

Change this line

Code: Select all

Key_Wait_Multi(keys, options := "") {
to

Code: Select all

Key_Wait_Multi(keys, options := "T2 V") {
User avatar
yfjuu6
Posts: 125
Joined: 28 Apr 2023, 15:28

Re: KeyWait for multiple keys ??

24 Apr 2024, 06:13

@ntepa
How to send a listed key if pressed e.g :
something like this :

Code: Select all

if Key_Wait_Multi("a{SC34}{F12}") {
      if "a" is pressed send("a")
      if "SC34" is pressed send("{SC34}")
      if "F12" is pressed send("{F12}")
      etc ...
}
else
     MsgBox("you didn't press any of the listed keys")
ntepa
Posts: 432
Joined: 19 Oct 2022, 20:52

Re: KeyWait for multiple keys ??  Topic is solved

24 Apr 2024, 12:27

Try this:

Code: Select all

Key_Wait_Multi(keys, &pressedKey?, options := "T2 V") {
    ih := InputHook("L1 " options)
    ih.KeyOpt(keys, "ES")
    ih.Start()
    ih.Wait()
    pressedKey := (StrLen(ih.EndKey) > 1) ? "{" ih.EndKey "}" : ih.EndKey
    return (ih.EndReason = "EndKey")
}

if Key_Wait_Multi("a{SC34}{F12}", &pressedKey) {
    Send(pressedKey)
} else {
    MsgBox("you didn't press any of the listed keys")
}

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: Descolada, Draken, RussF and 32 guests