GetKeyState inside of function

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
Scr1pter
Posts: 1272
Joined: 06 Aug 2017, 08:21
Location: Germany

GetKeyState inside of function

Post by Scr1pter » 01 Feb 2023, 18:21

Hello,

I'm playing around with custom modifier keys, so instead of Shift+4 something like F1+4
Normally I always did it this way:

Code: Select all

F1::
while GetKeyState("F1") ; Works fine
{
  modifier := 1
}
modifier := 0
return

#if modifier

4::
Send hello
return

6::
Send test

#if
I'm wondering if I could also create a function which does GetKeyState.
After several tries it did not work.

Code: Select all

F2::
Hold_Key()
return

Hold_Key()
{
  while GetKeyState("F2", "P") ; No effect
  {
    modifier := 1
  }
  modifier := 0
}
Is there something I have to do additionally?
Looks like the function only gets called, not actually held.

Thanks for any help and best regards
Please use [code][/code] when posting code!
Keyboard: Logitech G PRO - Mouse: Logitech G502 LS - OS: Windows 10 Pro 64 Bit - AHK version: 1.1.33.09

User avatar
DuckingQuack
Posts: 219
Joined: 20 Jan 2023, 18:20

Re: GetKeyState inside of function

Post by DuckingQuack » 01 Feb 2023, 18:36

Place this at the top of the script with the function

Code: Select all

Global modifier
In the second one, all of your modifier variables are in brackets making it a local variable (at least that's my understanding). But in the first one, at least one is not in brackets.

Edit: clarity
Edit: suggestion: maybe the second script doesn't require both modifiers to be in the brackets?
Edit: I tested my suggestion and it was not good... Global seems to be the best thing I got
Best of Luck,
The Duck

ntepa
Posts: 426
Joined: 19 Oct 2022, 20:52

Re: GetKeyState inside of function

Post by ntepa » 01 Feb 2023, 19:25

You need to declare modifier as global to use it in a function.
You can also use KeyWait instead of while loop.

Code: Select all

F2::
Hold_Key()
return

#if modifier

4::
Send hello
return

6::
Send test

#if


Hold_Key()
{
    global modifier
    modifier := 1
    KeyWait F2
    modifier := 0
}

User avatar
Scr1pter
Posts: 1272
Joined: 06 Aug 2017, 08:21
Location: Germany

Re: GetKeyState inside of function

Post by Scr1pter » 02 Feb 2023, 17:18

Hello and thank you both for your help.

Adding global and using just KeyWait had a positive effect :thumbup:

I have one last wish regarding this topic.
My idea is that when I press a key for a short time (e.g. less than 200 ms), that it has its normal function.
This can be useful for keys like F11 or F12, where you normally don't hold these keys pressed.

While holding these keys, I would like to use them as modifiers.
The modifier thing works now, but I'm not sure how to achieve the short key press thing, yet I'm sure it can work.

Code: Select all

5::
Short_Long(func("Key_Normal").bind(A_ThisHotkey), func("Hold_Key").bind(A_ThisHotkey), A_ThisHotkey)
return

Short_Long(param1, param2, key) ; Works
{
  KeyWait, %key%, T0.2 ; Prüfen, ob die Taste weniger als 200 ms lang gedrückt gehalten wurde
  if ErrorLevel = 1 ; Skript 2 - Taste wurde lang gedrückt gehalten:
  {
    param2.(myObject) ; Taste lang gedrückt: Funktion 2
    KeyWait, %key% ; Warten, dass die Taste erneut gedrückt wird
  }
  else ; Skript 1 - Taste wurde kurz gedrückt:
  {
    param1.(myObject) ; Taste kurz gedrückt: Funktion 1
  }
}
Key_Normal(key) ; Doesn't work
{
  global ; Probably no effect
  ;MsgBox, %key% ; MessageBox appears and shows correct key ("5" in this case)
  Send {%key%} ; Key does not get sent
}
Hold_Key(key) ; Works perfectly
{
  global
  
  modifier := 1
  KeyWait, %key% ; After key was released:
  modifier := 0
}

#if modifier

4::
Send hello ; Works
return

6::
Send test ; Works
return

#if
Thanks again for any help and best regards
Please use [code][/code] when posting code!
Keyboard: Logitech G PRO - Mouse: Logitech G502 LS - OS: Windows 10 Pro 64 Bit - AHK version: 1.1.33.09

User avatar
DuckingQuack
Posts: 219
Joined: 20 Jan 2023, 18:20

Re: GetKeyState inside of function

Post by DuckingQuack » 02 Feb 2023, 18:46

https://www.autohotkey.com/docs/v1/lib/SetTimer.htm#ExampleCount
This is a link to an example on the SetTimer page that appears to be what you're after and only needs some slight tweaking to fit your needs. I'll add an edit to this post if I make time to tweak it.

edit:
That was easier than I expected. I don't code much in V1, so there are likely mistakes and I didn't test it because I'm being lazy, but this should at least give you a ballpark idea of where to go!
P.S. All I did was delete the sections of the example that weren't what you needed, then changed the keys.

Code: Select all

#Requires AutoHotkey v1.1.33
Esc::ExitApp

F11::
SetTimer, KeyWinC, -200
return

KeyWinC:
if GetKeyState("F11", "P")
{
    ;modifier stuff here
}
else send {F11}
return
Best of Luck,
The Duck

User avatar
Scr1pter
Posts: 1272
Joined: 06 Aug 2017, 08:21
Location: Germany

Re: GetKeyState inside of function

Post by Scr1pter » 03 Feb 2023, 19:48

Thanks for your reply.

The first try did not work, but I had another idea which worked.

I just added a :

Code: Select all

5 up::
return
For whatever reason it works now. :think:
- I press 5 for a very short time (less than 200ms), then it outputs 5.
- I press and hold 5, and it becomes a modifier.

Well, it works. ;)

Best regards
Please use [code][/code] when posting code!
Keyboard: Logitech G PRO - Mouse: Logitech G502 LS - OS: Windows 10 Pro 64 Bit - AHK version: 1.1.33.09

Post Reply

Return to “Ask for Help (v1)”