Key press F1 and ahk holds it until I press it again

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
markbravo0312
Posts: 2
Joined: 28 Mar 2017, 13:20

Key press F1 and ahk holds it until I press it again

26 Apr 2017, 18:32

I am new at this. I am kind of useless. I wanted to make a script where whenever I pressed F1 ahk would hold it for me until I pressed F1 again. I don't know if it is possible or if anyone could help me out. All help appreciated.
User avatar
Exaskryz
Posts: 2882
Joined: 17 Oct 2015, 20:28

Re: Key press F1 and ahk holds it until I press it again

26 Apr 2017, 23:13

I am going to give you links to several documents pages that will be pertinent to your project. I encourage you to take a stab at learning this stuff, because AHK is meant to be accessible for people without any coding background. Within the documentation, there are examples at the bottom of the page. You may need to first read before anything the beginner's tutorial - read how to create a script if you have not used one before.

So here are the first things to read to make your script:

Hotkeys
Send
return ; just put return on a line by itself to tell the hotkey to stop reading for further instructions

My first task to you would be make hotkeys a:: and b:: which send F1 into a down state and F1 into an up state. We'll combine them later, but let's just get the basics grasped.

Next, we want to transition one of the hotkeys into the F1 key. A little complication with this is that the Send command can trigger hotkeys itself, including it's own hotkey. We can prevent a hotkey from being triggered by the Send command by using the $ modifier.

Alright. Next, we want to merge these hotkeys; we'll eliminate the second hotkey. But how do we move the second hotkey's actions into the first? One way is to use an If/else to make the single hotkey perform one action or another depending on a condition. What is that condition? There are a couple options, but one way is to our own condition with a a custom variable that we will toggle the value of between true and false. The way we do that is using the assign := operator and the logical-not ! operator. toggle:=!toggle will change the value of the variable toggle from true to false, and vice versa. We can then use a statement like if toggle for one action, and then the else for another.

While it is not necessary in this example, I will also link you to Blocks. These are used with If/else statements, and other commands, to tie multiple lines of code to one If/else.

So, a quick run down of what I'm thinking your final code should look like. (It is possible to make more efficient code, but that is a lesson for another day.) The first line should be your hotkey. The second line should be toggle:=!toggle or the name of any variable you please, like cat, F1_change, or myFirstVariable. The next few lines should be If/else statements that will either Send the F1 key into a down state or an up state. Then finally, end with a return.
markbravo0312
Posts: 2
Joined: 28 Mar 2017, 13:20

Re: Key press F1 and ahk holds it until I press it again

27 Apr 2017, 13:17

Thanks for the fast response. So far I have done this. Sorry if it blinds you and of course it's not working and I don't know why.:
#NoEnv
F1:: Hotkey, F1, Toggle
toggle:=!toggle

Return
neomulemi6
Posts: 216
Joined: 30 Jun 2016, 06:01

Re: Key press F1 and ahk holds it until I press it again

27 Apr 2017, 16:46

This should work.

Code: Select all

$F1::
If (GetKeyState("F1","P")) {
	Send {F1 up}
} Else {
	Send {F1 down}
}
Return
User avatar
Exaskryz
Posts: 2882
Joined: 17 Oct 2015, 20:28

Re: Key press F1 and ahk holds it until I press it again

27 Apr 2017, 19:44

neo's solution should not work. I'll address both replies.

1) @mark, check out that Hotkey documentation. There's both a "hotkey" that is "written out" in the script, and there is also a Hotkey command. They're somewhat different things. You don't need the Hotkey command - so in other words, you don't need to put the word "Hotkey" in your script.

What you would have at just

Code: Select all

F1::
toggle:=!toggle
return
Would be a good start.

You would then just need to add in the if toggle, associated action Send {F1 down}, the else, and Send {F1 up} as demonstrated by neo.

Neo took an alternative approach. Instead of using a toggle variable, it would check for the key state of $F1. This should not work because the "P" parameter would check the physical state of the key - you press the F1 hotkey to activate it, and the very first line is polling the the state of the F1 key. Because it is likely to still be held down - you haven't had time to release the key before the script starts - then that If statement is always going to come back true and send F1 into the up or released position.

What should work though is using If (GetKeyState("F1")){ which checks the logical state, not the physical state of the key. The F1 hotkey on its own already makes sure that the native action of the key - being put into a logical down state when pressed - does not happen. So it is within your script when you use Send {F1 down} that the logical state would return True, and it is when you use Send {F1 up} that the logical state would return False.

I didn't address the GetKeyState() route at first, because that introduces functions and expression syntax. (Notice how within the function, we are putting values in quotes? That's to pass literal values. Otherwise, a function expects the name of a variable and it will use the value of that variable... But now that I think about it, the := operator does the same and expects the name of a variable, so it's already using expression syntax when we use toggle:=!toggle.)

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Proxima, Rohwedder and 303 guests