Reverse Key Pressing

Ask gaming related questions
Starker
Posts: 6
Joined: 27 Mar 2018, 18:39

Reverse Key Pressing

Post by Starker » 20 Sep 2018, 16:33

I am trying to make a script that:

- Sends the P (or any key) key as held down when the physical state of the Shift key is up

- Sends the P key as up when the physical state of the Shift key is held down.

- Essentially, I need script that reverses the function of the shift key.

There is no toggle in the script.
Rohwedder
Posts: 1360
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Reverse Key Pressing

Post by Rohwedder » 22 Sep 2018, 04:35

Hallo,
Set CapsLockState on
or try:

Code: Select all

+p::Send {p DownR}
p::Send +{p DownR}
*p Up::Send {p Up}
Starker
Posts: 6
Joined: 27 Mar 2018, 18:39

Re: Reverse Key Pressing

Post by Starker » 25 Sep 2018, 16:24

Rohwedder wrote:Hallo,
Set CapsLockState on
or try:

Code: Select all

+p::Send {p DownR}
p::Send +{p DownR}
*p Up::Send {p Up}
Sorry, my explanation was misleading. Shift should not be a modifier for P in this script.

I am trying to have P sent as held down always unless Shift is held down. Once Shift is released, P returns to the down position.
Starker
Posts: 6
Joined: 27 Mar 2018, 18:39

Re: Reverse Key Pressing

Post by Starker » 25 Sep 2018, 16:30

I have come up with this so far:

Code: Select all

KeyState := GetKeyState("Shift")
loop{
	if (KeyState = 1){
		send {p up}
	}
	else{
		send {p down}
	}
sleep 20
}
It sends P as down continuously, but Shift doesn't break the loop.
Onimuru
Posts: 33
Joined: 08 Sep 2018, 18:35
GitHub: Onimuru

Re: Reverse Key Pressing

Post by Onimuru » 25 Sep 2018, 17:33

Starker wrote:I have come up with this so far:

Code: Select all

KeyState := GetKeyState("Shift")
loop{
	if (KeyState = 1){
		send {p up}
	}
	else{
		send {p down}
	}
sleep 20
}
It sends P as down continuously, but Shift doesn't break the loop.
Try this:

Code: Select all

#SingleInstance Force
SendMode Input

SetTimer, p_L, On

Exit

p_L:
	Send {p Down}
	Sleep 20
	Return

+Esc::ExitApp	;Shift + Escape to exit the script.

$LShift::
	SetTimer, p_L, Off
	KeyWait, Shift
	SetTimer, p_L, On
	Return
And to tie it to a window:

Code: Select all

#SingleInstance Force
SendMode Input

SetTimer, p_L, On

Exit

p_L:
	If !WinActive("ahk_class Notepad++")		;Change the class here to your game.
		WinWaitActive, ahk_class Notepad++
	Send {p Down}
	Sleep 20
	Return

#If WinActive("ahk_class Notepad++")
LShift::
	SetTimer, p_L, Off
	KeyWait, Shift
	SetTimer, p_L, On
	Return
#If

+Esc::ExitApp
Last edited by Onimuru on 25 Sep 2018, 17:53, edited 2 times in total.
swagfag
Posts: 1214
Joined: 11 Jan 2017, 17:59

Re: Reverse Key Pressing

Post by swagfag » 25 Sep 2018, 17:39

Code: Select all

#InstallKeybdHook

loop{
	KeyState := GetKeyState("Shift")
	if (KeyState = 1){
		send {p up}
	}
	else{
		send {p down}
	}
sleep 20
}
Rohwedder
Posts: 1360
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Reverse Key Pressing

Post by Rohwedder » 26 Sep 2018, 00:29

Hallo,
try:

Code: Select all

SendInput, {p DownR}
Return
*p::Return
~*Shift::SendInput, {p Up}
~Shift Up::SendInput, {p DownR}
Starker
Posts: 6
Joined: 27 Mar 2018, 18:39

Re: Reverse Key Pressing

Post by Starker » 29 Sep 2018, 19:26

Onimuru wrote:
Starker wrote:I have come up with this so far:

Code: Select all

KeyState := GetKeyState("Shift")
loop{
	if (KeyState = 1){
		send {p up}
	}
	else{
		send {p down}
	}
sleep 20
}
It sends P as down continuously, but Shift doesn't break the loop.
Try this:

Code: Select all

#SingleInstance Force
SendMode Input

SetTimer, p_L, On

Exit

p_L:
	Send {p Down}
	Sleep 20
	Return

+Esc::ExitApp	;Shift + Escape to exit the script.

$LShift::
	SetTimer, p_L, Off
	KeyWait, Shift
	SetTimer, p_L, On
	Return
And to tie it to a window:

Code: Select all

#SingleInstance Force
SendMode Input

SetTimer, p_L, On

Exit

p_L:
	If !WinActive("ahk_class Notepad++")		;Change the class here to your game.
		WinWaitActive, ahk_class Notepad++
	Send {p Down}
	Sleep 20
	Return

#If WinActive("ahk_class Notepad++")
LShift::
	SetTimer, p_L, Off
	KeyWait, Shift
	SetTimer, p_L, On
	Return
#If

+Esc::ExitApp

I am not sure how I am supposed to use this. It appears that it will only run when Sven Co-op is thw active window. To run the script, I must clock out of Sven Co-op, thus Sven Co-op is no longer active and the script instantly closes.

However, I tried just the first block you wrote and that also instantly crashes.

I replaced all "ahk_class" with "Sven Co-op" which is the name of the game's window.
Starker
Posts: 6
Joined: 27 Mar 2018, 18:39

Re: Reverse Key Pressing

Post by Starker » 29 Sep 2018, 19:31

Rohwedder wrote:Hallo,
try:

Code: Select all

SendInput, {p DownR}
Return
*p::Return
~*Shift::SendInput, {p Up}
~Shift Up::SendInput, {p DownR}
This crashes instantly when run.
Starker
Posts: 6
Joined: 27 Mar 2018, 18:39

Re: Reverse Key Pressing

Post by Starker » 29 Sep 2018, 19:35

swagfag wrote:

Code: Select all

#InstallKeybdHook

loop{
	KeyState := GetKeyState("Shift")
	if (KeyState = 1){
		send {p up}
	}
	else{
		send {p down}
	}
sleep 20
}
This also crashes instantly.

At the risk of sounding whiney, the scripts given to me by those on the AHK Subreddit actually ran, even though they did not fully function.
Should I expect people to post scripts here without having run them themselves?

Yes, I have reinstalled AHK.
Onimuru
Posts: 33
Joined: 08 Sep 2018, 18:35
GitHub: Onimuru

Re: Reverse Key Pressing

Post by Onimuru » 29 Sep 2018, 19:38

You need to debug your issue yourself, I tested the code I posted and it works fine. It may be UAC or something. In fact, all the code posted here is good. I'm not sure if you want p to be sent continuously (ppppppppp) or as held down but once you fix the crashing issue you're having, you will find the code you need to achieve what you want in the examples.
Post Reply

Return to “Gaming”