Problem checking previously key Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
virtualauto
Posts: 26
Joined: 19 Jun 2016, 01:04

Problem checking previously key

19 Apr 2018, 19:59

I want the script to take some action when the right mouse button is clicked but only if the prior key was the left button. It doesn't work: the message box is always shown when I click the right button regardless of what key is previously pressed.

Code: Select all

MouseIsOver(WinTitle) {
    MouseGetPos,,, Win
    return WinExist(WinTitle . " ahk_id " . Win)
}

#If MouseIsOver("ahk_class CabinetWClass")
	RButton::
		if(%A_PriorKey% = LButton){
			MsgBox, Hello
		}
		Return
#If
User avatar
Xtra
Posts: 2750
Joined: 02 Oct 2015, 12:15

Re: Problem checking previously key  Topic is solved

19 Apr 2018, 21:11

There are 2 issues with this syntax: if(%A_PriorKey% = LButton)

1) When using expression syntax ie: ( <-- code inside --> )
You dont need %'s to deref the variable unless the variable value is the name of another variable that has a value. (double deref)

2) LButton is not a variable so when comparing inside of an expression using = you want a string --> "LButton"

The syntax you should be using:
if (A_PriorKey = "LButton")

HTH
virtualauto
Posts: 26
Joined: 19 Jun 2016, 01:04

Re: Problem checking previously key

19 Apr 2018, 21:20

Xtra wrote:There are 2 issues with this syntax...
Thanks for the info BUT it still does not work: the message box keeps being shown everytime I right click regardless of the prior pressed key. Should I use A_PriorHotkey?
Last edited by virtualauto on 19 Apr 2018, 21:27, edited 1 time in total.
poetbox
Posts: 112
Joined: 18 Apr 2018, 20:47

Re: Problem checking previously key

19 Apr 2018, 21:27

1.For this variable to be of use, the keyboard or mouse hook must be installed and key history must be enabled.
2.You can show the value of "A_PriorKey" before the "hello" msg
poetbox
Posts: 112
Joined: 18 Apr 2018, 20:47

Re: Problem checking previously key

19 Apr 2018, 21:32

you can get the difference below
if(A_PriorKey =" LButton")
{
MsgBox, %A_PriorKey%Hello
}
=======================
if(A_PriorKey <>" LButton")
{
MsgBox, %A_PriorKey%Hello
}
virtualauto
Posts: 26
Joined: 19 Jun 2016, 01:04

Re: Problem checking previously key

19 Apr 2018, 21:36

Thanks guys, the problem was that when I was clicking the message box it was taking it into account to trigger the next message box. I don't know why, because after dismissing the message box I was pressing everytime another keyboard key with the same result. I guess is an error from AutoHotkey. Thank you again!
poetbox
Posts: 112
Joined: 18 Apr 2018, 20:47

Re: Problem checking previously key

19 Apr 2018, 21:58

#InstallMouseHook
#InstallKeybdHook
#KeyHistory 100
Add above code before you code and test again.
scriptor2016
Posts: 851
Joined: 21 Dec 2015, 02:34

Re: Problem checking previously key

20 Apr 2018, 00:07

a little late to the party but this should work as well:

Code: Select all

~lbutton::
KeyWait, lbutton, T2
KeyWait, rbutton, T2 D

if ErrorLevel = 0 
{
}
else if ErrorLevel = 1 
{   
Return
}

 

KeyWait, rbutton, T2
if ErrorLevel = 0 
{
MsgBox, You Pressed Lbutton, then RButton 
}
else if ErrorLevel = 1 
{   
 
}
return

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Freddie, haomingchen1998, mmflume, scriptor2016, ShatterCoder and 91 guests