Reading keyboard inputs and inverting them

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

Reading keyboard inputs and inverting them

07 Jan 2018, 11:32

Hi,

I want to write a script which reads my keyboard inputs (e.g. 5x arrow right, 2x arrow up)
and inverts these commands (in that case 2x arrow down, 5x arrow left).
So if I move from a couple of cells in Excel, it should go the way back to the beginning.

My concrete idea is this:
1. Pressing a key to start the recording, F1
2. Moving with the arrow keys
3. Pressing again a key (can be also F1) to store the current movement and to start another recording
4. Moving with arrow keys.
.
.
.
Pressing a key to finish the recording, F2 (wrongly shown in the picture)

Graphically it would look like this one:
Image
Now I want to be able to go back to point 1, backwards.

Recording would be done with KeyHistory I guess.
But regarding the inverting I have no idea.
I only found inverting mouse movements, which is not my aim.

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
Exaskryz
Posts: 2882
Joined: 17 Oct 2015, 20:28

Re: Reading keyboard inputs and inverting them

07 Jan 2018, 13:13

Untested code.

Code: Select all

~Left::string.="{Right}"
~Right::string.="{Left}"
~Up::string.="{Down}"
~Down::string.="{Up}"

F1:: ; capture the recording in reverse
If string
{
counter++ ; keeps track of each segment
segment_%counter%:=string ; stores the LRUD into this segment
}
string:="" ; 
return

F2:: ; play back
GoSub, F1 ; repeat the same actions as F1 because we need to save this final segment
Send % segment_%counter%
counter--
return
Try that out. The idea is you just capture the recording in reverse and save the end of a segment with F1. I realize I didn't actually make it require F1 to start recording; you could do that by making F1 turn on the hotkeys (and then F2 turns off the hotkeys) with the Hotkey command. So maybe you actually want an implementation like this:

Code: Select all

F1:: ; capture the recording in reverse
If string
{
counter++ ; keeps track of each segment
segment_%counter%:=string ; stores the reversed keys into this segment
string:=""
}
else ; string is empty, so you just started this process or haven't recorded anything new
{
Hotkey, ~Left, Process
Hotkey, ~Right, Process
Hotkey, ~Up, Process
Hotkey, ~Down, Process
hotkeys_enabled:=true
} 
return

F2:: ; play back
; We don't GoSub F1 here because we don't want the Hotkeys to be turned on unnecessarily.
If hotkeys_enabled ; not sure if we really need to worry about this or if I have done the right implementation to stop recording
{
Hotkey, ~Left, Off
Hotkey, ~Right, Off
Hotkey, ~Up, Off
Hotkey, ~Down, Off
hotkeys_enabled:=false
}
If string ; if there was a recording not yet saved, we save it
{
counter++ ; keeps track of each segment
segment_%counter%:=string
string:=""
}
Send % segment_%counter%
counter--
return

Reverse:
If (A_ThisHotkey="~Left")
string.="{Right}"
else If (A_ThisHotkey="~Right")
string.="{Left}"
else If (A_ThisHotkey="~Up")
string.="{Down}"
else
string.="{Up}"
return
So F1 can enable recording, subsequent F1 will record each segment. F2 will stop recording and start playing the segments in reverse.
User avatar
Capn Odin
Posts: 1352
Joined: 23 Feb 2016, 19:45
Location: Denmark
Contact:

Re: Reading keyboard inputs and inverting them

07 Jan 2018, 13:19

This should get you started, you only need to make the logic for the multiple recording sessions.

Code: Select all

opposite := {"Down" : "Up", "Up" : "Down", "Right" : "Left", "Left" : "Right"}

F1::keys := Record()

p::Play(keys)
r::Reverse(keys)

Play(keys) {
	for i, key in keys {
		Send, % "{" key "}"
		Sleep, 200
	}
}

Reverse(keys) {
	Global opposite
	loop % keys.Length() {
		Send, % "{" opposite[keys[keys.Length()-A_Index]] "}"
		Sleep, 200
	}
}

Record() {
	keys := []
	loop {
		Input, key, L1, {Down}{Up}{Left}{Right}{F1}
		key := SubStr(ErrorLevel, InStr(ErrorLevel, ":")+1)
		ToolTip, % key
		if(key = "F1") {
			Break
		} else if(key = "down" || key = "up" || key = "right" || key = "left") {
			keys.Push(key)
		}
	}
	return keys
}
Please excuse my spelling I am dyslexic.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Bing [Bot], Mateusz53, mikeyww, MrHue and 320 guests