How to Send NUMPAD9 when Joystick up Right

Ask gaming related questions (AHK v1.1 and older)
baba_anubis
Posts: 10
Joined: 08 Dec 2016, 19:39

How to Send NUMPAD9 when Joystick up Right

12 Oct 2021, 17:29

Hi, I hope someone can help me...

I need a script which does the following:

When I move the joystick up I want it to send NUMPAD 8

When I move the Joystick Right I want it so send NUMPAD 6

When I move the Joystick UP RIGHT I want it so send Numpad 9

I need it for some old game, please bare in mind that this is 2 player game so I want to have a unique script for each joystick.


Here is a script which I always duplicate for diffrent games with diffrent keys...however this one works great but it doesn't take into account to send NUMPAD 9 when I move the joystick UP & RIGHT

(I didn't write this script, I believe someone here helped me long ago...not sure...)

Code: Select all

JoyNum = 1 ; put a number if you have more than 1 joystick
XLowKey = left
XHighKey = right
YLowKey = up
YHighKey = down
Axis = XY ; these are the axises we're using
RepeatRate = 500 ~ 50 ; slowest and fastest delays between repeats (in ms)
; WARNING: no newbies below this line
#Persistent
SendMode, Input ; es muy rapido
StringSplit, r, RepeatRate, ~, %A_Space% ; moves the two numbers from
; RepeatRate into r1 and r2 for use later in the script
SetTimer, WatchAxis, 10
WatchAxis:
Loop, Parse, Axis ; Sets the variable A_LoopField to the current axis
; this next if statement is the kicker, it is one expression evaluated like so:
; first, lets say that we're dealing with the X axis, so the expression resolves
; !qX first, lets say it resolves to 1 (which it does when qX is either empty or 0)
; then, the getkeystate is resolved, and the JoyX axis is retrieved. by default
; joystick axises go from 0 to 100, with 50 being in the middle, so this part
; takes that number, subtracts 50 from it, and stores the result in jX
; then the absolute value of jX is stored in dX
; then dX is compared to the number 7, and if it is greater, then a 1 gets
; stored in qX, otherwise, a 0 gets stored in qX
; remember how the expression already resolved !qX at the beginning?
; well, !qX will be equal to the new value of qX if and only if that
; value changes from 0 to 1 or from 1 to 0, thusly, the overall statement
; only resolves to 'true' when the axis changes between "close to center" and
; "not close to center"
If (!q%A_LoopField%) = q%A_LoopField% := (7 < d%A_LoopField% := ""
. Abs(j%A_LoopField% := GetKeyState(joynum "Joy" A_LoopField, "P") - 50))
{
; so, this block will execute when the axis changes status
; and the variable qX can be thought of as meaning "The axis is not close to the center"
; So, the next if statement checks whether qX is true, and if it is, then
; send a certan keydown event. That certan key is chosen by the ternary operator
; and is stored in the variable XKey. The ternary operator checks whether
; jX is above or below zero (remember when the axis position was stored in jX?)
   IfEqual, q%A_LoopField%, 1, Send, % "{Blind}{"
   . (%A_LoopField%Key := j%A_LoopField%<0 ? ""
   . %A_LoopField%LowKey : %A_LoopField%HighKey) " DownTemp}"
; But, if qX is not 1 (if the axis is close to center", then
; release the key that was previously held down
   Else Send % "{Blind}{" %A_LoopField%Key " Up}"
; update a variable to hold a time that we'll refer to as 'then'
   Delay%A_LoopField% := A_TickCount
}
; This next line will execute if, during this particular running of
; this subroutine, the current axis has not changed between being
; "close to the center" and "not close to the center"
; so usually this line gets executed (about 99.9% of the time)
; Anyways, the first if command checks whether the current axis is
; "not close to the center", which is the first part of checking whether
; to repeat a keystroke. Then it checks A_TickCount, which, in a certain
; sense, can be thought of as being equal to 'now'. So, the next part
; compares 'now' to 'then' plus a certain amount, and if it is greater
; then enough time has elapsed between 'then' and 'now' to allow another
; keystroke to be sent. That certain number is between the two numbers
; defined above in the RepeatRate variable
Else IfEqual, q%A_LoopField%, 1, IfGreater, A_TickCount, % ""
. Delay%A_LoopField% + r1 - ((d%A_LoopField%-8)/42)*(r1-r2)
{
;   Send % "{Blind}{" %A_LoopField%Key " Up}" ; uncomment to activate full key repeat
; typical keyboard repeating is just a series of keydown events. Some games
; want an 'up' event before they'll recognize that the key is being spammed
; usually this is not an issue unless you need true rapid-fire for these keys
   Send % "{Blind}{" %A_LoopField%Key " DownTemp}"
; update a variable to hold a time that we'll refer to as 'then' next time around
   Delay%A_LoopField% := A_TickCount
}
return


1joy1::
Send {; down}   ; Press the RALT down.
SetTimer, WaitFor1joy1, 50  ; Reduce the number 30 to 20 or 10 to send keys faster. Increase it to send slower.
return

WaitFor1joy1:
if not GetKeyState("1joy1")  ; The button has been released.
{
    Send {; up}  ; Release the RALT.
    SetTimer, WaitFor1joy1, off  ; Stop monitoring the button.
    return
}
; Since above didn't "return", the button is still being held down.
Send {; down}  ; Send another RALT keystroke.
return
Thank you


Update I tried this code: NUMPAD 8, 6, 4 , 2 work, NUMPAD 9 doesn't....

Code: Select all

JoyNum = 1 ; put a number if you have more than 1 joystick
#Persistent  ; Keep this script running until the user explicitly exits it.
SetTimer, WatchAxis, 5
return

WatchAxis:
JoyX := GetKeyState("JoyX")  ; Get position of X axis.
JoyY := GetKeyState("JoyY")  ; Get position of Y axis.
KeyToHoldDownPrev := KeyToHoldDown  ; Prev now holds the key that was down before (if any).

if (JoyX > 70)
    KeyToHoldDown := "NUMPAD6"
else if (JoyX < 30)
    KeyToHoldDown := "NUMPAD4"
else if (JoyY > 70)
     KeyToHoldDown := "NUMPAD2"
else if (JoyY < 30)
    KeyToHoldDown := "NUMPAD8"
else if (JoyX > 70 and JoyY < 30)
    KeyToHoldDown := "NUMPAD9"
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


Update 2:

I was able to solve it here is how:

Need to send NUMPAD 9 when joystick UP and Right
viewtopic.php?f=76&t=95494&p=424698#p424698

Return to “Gaming Help (v1)”

Who is online

Users browsing this forum: No registered users and 40 guests