Axis Pushes Keyboard Keys When Axis Is Moved

Ask gaming related questions (AHK v1.1 and older)
codetrucker
Posts: 27
Joined: 29 May 2019, 01:30

Axis Pushes Keyboard Keys When Axis Is Moved

29 May 2019, 02:11

Hello,

After days of Internet research and manifold experimentation I have come up with a big fat zero. Perhaps this is because it is not possible. I am not lazy and I have been a coder for a career. But, for the life of me I don't know what else to do. Further, while I was a pretty fair coder in my day, these days I have a very difficult time with multi-matrix structures and get confused very easily. I have been working on the below script for three days. Hence, my reason for finally revealing my ignorance to this forum. Enough of the disclaimer, but I am grateful for your patience.

The script code below contains the rationale in the initial commentary. I hope this makes sense? So, why doesn't it work? I have tried to offer a context in the comments, but I will be happy to expound or clarify on anything.

Thank you.

Code: Select all

/*
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
LOCOMOTIVE CAB CONTROLS FOR RXR ENGINEERS
=========================================
The intent of this script is to "catch" axis movement for the purposes of creating a train locomotive control panel which normally consistes of the following...
-- Throttle Lever  - it makes the train go.
-- Reverser - directs the train to go forward or backwars as well as making the best use of steam output from steam locomotives.
-- Brake Lever - Makes the train slow and stop.
-- Dynamic Brake Lever - a "running" brake which uses the electomagnetic force of the wheel's motors to create electro-resistance against the direction of wheel spin.

This script is a modification of the "Remapping" script. 

Simply, the script polls the controller axes continuously approx. every 500  milliseconds or thereabouts (this should be user-adjustable). 
There are 1 of 6 actions triggered every time a lever is moved. ..
-- Pushing the 1st lever forward enters... "W" key press and release (Reverser increase).
-- Pull the 1st lever rearward enters..... "S" key press and release (Reverser decrease).
-- Pushing the 2nd lever forward enters... "A" key press and release (Throttle decrease). ***Note: the 2nd Lever increase/decrease is opposite Lever 1 and Lever 3.
-- Pull the 2nd lever forward enters...... "D" key press and release.
-- Pushing the 3rd lever forward enters... "'" (apostrophe) key press and release.
-- Pull the 3rd lever forward enters...... ";" (semicolon) key press and release.

Effectively, this means as the levers are continuously moved the intended key is continuously pressed (but not held down).  


Logic (as I understand?)
-----------------------
[I will use Lever 2 (Thrttle) as example.
Loop
   Read controller axis value, "Joy2"
   Move value of "Joy" into variable, "Joy2_Old"
   If Joy2 > Joy2_Old then 			; Poll for change.
	Press and release "A" key		; Decrease Throttle.
   Elseif Joy2 = Joy2_Old then 			; No movement.
	Exit
   If Joy2 < Joy2_Old then			; Poll for change.
	Press and release "D" key		; Increae Throttle.
End Loop

For simplicity, I am only using one lever in this script. Once I get one lever working, I'm sure I can get the other two working as well (I hope?).

I have tried what you see below, but it doesn't work. Please help.

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
*/


#Persistent  ; Keep this script running until the user explicitly exits it.
SetTimer, WatchAxis, 5
return


WatchAxis:
; Lever 2 - Throttle
Joy2X := GetKeyState("Joy2X")  		; Get position of 2X axis.
Joy2Y := GetKeyState("Joy2Y")  		; Get position of 2Y axis.
Joy2X_Old := Joy2X			
Joy2Y_Old := Joy2Y
KeyToHoldDownPrev := KeyToHoldDown  	; Prev now holds the key that was down before (if any).

if (Joy2X > Joy2X_Old)			
    KeyToHoldDown := "A"
else if (Joy2X < Joy2X_Old)		
    KeyToHoldDown := "D"
else
    KeyToHoldDown := ""

if (KeyToHoldDown = KeyToHoldDownPrev)  ; The correct key is already down (or no key is needed).
    return  ; Do nothing.

; Otherwise, release the previous key and press down the new key:
SetKeyDelay -1  ; Avoid delays between keystrokes.
if KeyToHoldDownPrev   ; There is a previous key to release.
    Send, {%KeyToHoldDownPrev% up}  ; Release it.
if KeyToHoldDown   ; There is a key to press down.
    Send, {%KeyToHoldDown% down}  ; Press it down.

return
codetrucker
Posts: 27
Joined: 29 May 2019, 01:30

Re: Axis Pushes Keyboard Keys When Axis Is Moved

29 May 2019, 03:30

Hi eC,

Thanks for the links. I did read up on UCR, but I couldn't get my noodle around what it said yet. I'll try again. I never heard of AHI though.

@ Anyone - I would appreciate some critique on my coding attempt. Was I at least thinking right or did I just type out garbage?

Calvin
codetrucker
Posts: 27
Joined: 29 May 2019, 01:30

Re: Axis Pushes Keyboard Keys When Axis Is Moved

29 May 2019, 03:38

Hi eC,

Oops! I thought you were only posting back links. My browser only showed me your links. I didn't see,"What, exactly does not work?" until just now. I misunderstood. Sorry.

What isn't working is there are no key presses when I move the controller axis lever. Does it work for you?

Calvin
User avatar
evilC
Posts: 4823
Joined: 27 Feb 2014, 12:30

Re: Axis Pushes Keyboard Keys When Axis Is Moved

29 May 2019, 03:47

Joy2X is invalid syntax
Joystick 2, axis x is 2JoyX
What I was looking forwas something like "the variable Joy2X is always empty"
Just saying "it doesn't work" gave me zero useful info - as a career coder you should really know that
Last edited by evilC on 29 May 2019, 03:52, edited 3 times in total.
codetrucker
Posts: 27
Joined: 29 May 2019, 01:30

Re: Axis Pushes Keyboard Keys When Axis Is Moved

29 May 2019, 03:48

Okay, I'll run with that and report back. Thanks.
codetrucker
Posts: 27
Joined: 29 May 2019, 01:30

Re: Axis Pushes Keyboard Keys When Axis Is Moved

29 May 2019, 04:07

Yep, you are absolutely right. Ha! I used to get onto folks when they just said "it doesn't work." FYI - my career was pre-Y2K in COBOL/CICS (my favorite)/DB2. I am sort of a dinosaur. LOL. Anyway, I'll try again...

When I move the lever, nothing happens. Using a text editor for testing, it should "press" the A key, typing out repeating multiple "A" characters when I push the lever forward for as long as I push it forward and likewise type out a string of "D" characters when I pull the lever back. It should do this whenever the lever is moved forward and back.
Last edited by codetrucker on 05 Jun 2023, 18:53, edited 1 time in total.
codetrucker
Posts: 27
Joined: 29 May 2019, 01:30

Re: Axis Pushes Keyboard Keys When Axis Is Moved

29 May 2019, 04:26

I posted back with a img of the controller, but the forum intercepted it. There will probably be a duplicate.

What I also posted is the “Joy2X” was supposed to represent the second lever of the six levers on the controller. However, I recognized that I made an error since the levers are supposed to be listed as X, Y, Z, R, U and V, respectively. I should be referencing the second controller, second lever which should have been the Y axis which should be 1JoyY if I am understanding correctly.
codetrucker
Posts: 27
Joined: 29 May 2019, 01:30

Re: Axis Pushes Keyboard Keys When Axis Is Moved

29 May 2019, 04:49

evilC wrote:
29 May 2019, 04:35
No, 1JoyY is 1st stick
Use PJP Joyids app (Google it) to see joy ids
Maybe I'm just dense, but I searched for "PJP Joyids app" and only came up with "vJoy" and apps to renumber controllers.

FYI - I got the "1Joy" from the AHK, "Controller Test Script" from the AHK website. Is that script not the right one?
codetrucker
Posts: 27
Joined: 29 May 2019, 01:30

Re: Axis Pushes Keyboard Keys When Axis Is Moved

29 May 2019, 04:52

How can I read the values in variables? I used to use "watches" in VB4.
codetrucker
Posts: 27
Joined: 29 May 2019, 01:30

Re: Axis Pushes Keyboard Keys When Axis Is Moved

29 May 2019, 05:18

Thanks.

Just a quick reality check on my script.

-- Store the levers present value (position) in a "previous" variable.
-- Read the lever for any movement (different position).
-- If forward movement then press "A" key
-- If backward movement then press "D" key.
-- Repeat.

Is this logical? I just want to make sure I am not wasting your time.
codetrucker
Posts: 27
Joined: 29 May 2019, 01:30

Re: Axis Pushes Keyboard Keys When Axis Is Moved

29 May 2019, 06:55

evilC wrote:
29 May 2019, 05:06
Use a proper IDE such as scite4ahk or akstudio and use the debugger
Or use outputdebug command plus ms debugview
Or use the tooltip or msgbox commands
No success yet. I'll keep looking and report back once I make something work out. Thanks for the help.

Calvin
codetrucker
Posts: 27
Joined: 29 May 2019, 01:30

Re: Axis Pushes Keyboard Keys When Axis Is Moved

13 Jun 2019, 12:11

Update:
I am putting this on hold since I have discovered I am not understanding some basic(?) concepts. I will be posting a more specific subset of this scripts purpose as another thread and return to this project once I am successful. See, I haven't orphaned this thread. LOL
See you soon!
Nooneelse4ever
Posts: 8
Joined: 02 Jan 2021, 08:39

Re: Axis Pushes Keyboard Keys When Axis Is Moved

02 Jan 2021, 08:48

@evilC thanks, you made my day :D :bravo: :bravo: :bravo: :bravo: :bravo: :bravo: :dance: :dance: :dance: :dance: :superhappy: :superhappy: :superhappy: :superhappy: :superhappy: :superhappy:

Return to “Gaming Help (v1)”

Who is online

Users browsing this forum: No registered users and 63 guests