Main Restorations Software Audio/Jukebox/MP3 Everything Else Buy/Sell/Trade
Project Announcements Monitor/Video GroovyMAME Merit/JVL Touchscreen Meet Up Retail Vendors
Driving & Racing Woodworking Software Support Forums Consoles Project Arcade Reviews
Automated Projects Artwork Frontend Support Forums Pinball Forum Discussion Old Boards
Raspberry Pi & Dev Board controls.dat Linux Miscellaneous Arcade Wiki Discussion Old Archives
Lightguns Arcade1Up Try the site in https mode Site News

Unread posts | New Replies | Recent posts | Rules | Chatroom | Wiki | File Repository | RSS | Submit news

  

Author Topic: Autohotkey help please. [Solved]  (Read 8645 times)

0 Members and 1 Guest are viewing this topic.

monkey puzzle

  • Trade Count: (0)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 193
  • Last login:March 23, 2024, 02:11:59 pm
Autohotkey help please. [Solved]
« on: March 12, 2012, 06:19:53 pm »
I'm using Mala on a Windows xp machine and I have U360 Joysticks. I'm trying to assign 2 U360 buttons to exit all the emulators. In other words when I hold down player 1 start and player 1 button 1 I want it to register the ESCAPE key.

As well as this, I'm trying to assign some of the other buttons to resister as other keys in Visual pinball only.

Can somebody please help me to do this. I've tried researching this for myself but I feel that my brain will soon explode. I just need this explained to me please.
Any help would be appreciated.
« Last Edit: March 18, 2012, 08:05:37 am by monkey puzzle »

nitz

  • Trade Count: (+2)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 507
  • Last login:November 24, 2015, 07:57:29 pm
Re: Autohotkey help please.
« Reply #1 on: March 13, 2012, 12:16:46 am »
First, you'll need to determine which joystick buttons are player 1 start and button 1. Put this in a .ahk file and run it to figure that out.

Quote
; July 6, 2005: Added auto-detection of joystick number.
; May 8, 2005 : Fixed: JoyAxes is no longer queried as a means of
; detecting whether the joystick is connected.  Some joysticks are
; gamepads and don't have even a single axis.

; If you want to unconditionally use a specific joystick number, change
; the following value from 0 to the number of the joystick (1-16).
; A value of 0 causes the joystick number to be auto-detected:
JoystickNumber = 0

; END OF CONFIG SECTION. Do not make changes below this point unless
; you wish to alter the basic functionality of the script.

; Auto-detect the joystick number if called for:
if JoystickNumber <= 0
{
    Loop 16  ; Query each joystick number to find out which ones exist.
    {
        GetKeyState, JoyName, %A_Index%JoyName
        if JoyName <>
        {
            JoystickNumber = %A_Index%
            break
        }
    }
    if JoystickNumber <= 0
    {
        MsgBox The system does not appear to have any joysticks.
        ExitApp
    }
}

#SingleInstance
SetFormat, float, 03  ; Omit decimal point from axis position percentages.
GetKeyState, joy_buttons, %JoystickNumber%JoyButtons
GetKeyState, joy_name, %JoystickNumber%JoyName
GetKeyState, joy_info, %JoystickNumber%JoyInfo
Loop
{
    buttons_down =
    Loop, %joy_buttons%
    {
        GetKeyState, joy%a_index%, %JoystickNumber%joy%a_index%
        if joy%a_index% = D
            buttons_down = %buttons_down%%a_space%%a_index%
    }
    GetKeyState, joyx, %JoystickNumber%JoyX
    axis_info = X%joyx%
    GetKeyState, joyy, %JoystickNumber%JoyY
    axis_info = %axis_info%%a_space%%a_space%Y%joyy%
    IfInString, joy_info, Z
    {
        GetKeyState, joyz, %JoystickNumber%JoyZ
        axis_info = %axis_info%%a_space%%a_space%Z%joyz%
    }
    IfInString, joy_info, R
    {
        GetKeyState, joyr, %JoystickNumber%JoyR
        axis_info = %axis_info%%a_space%%a_space%R%joyr%
    }
    IfInString, joy_info, U
    {
        GetKeyState, joyu, %JoystickNumber%JoyU
        axis_info = %axis_info%%a_space%%a_space%U%joyu%
    }
    IfInString, joy_info, V
    {
        GetKeyState, joyv, %JoystickNumber%JoyV
        axis_info = %axis_info%%a_space%%a_space%V%joyv%
    }
    IfInString, joy_info, P
    {
        GetKeyState, joyp, %JoystickNumber%JoyPOV
        axis_info = %axis_info%%a_space%%a_space%POV%joyp%
    }
    ToolTip, %joy_name% (#%JoystickNumber%):`n%axis_info%`nButtons Down: %buttons_down%`n`n(right-click the tray icon to exit)
    Sleep, 100
}
return

So let's say player 1 start is Joy1 and player 1 button 1 is Joy2. To have a them send escape when you press them both, you would put this into a script.

Quote
$Joy1::
send {Joy1}
GetKeyState, state, Joy2
if state = D
send {esc}
return

If you hold down joy2 and press joy1, it should send escape. Otherwise both keys should function as normal.

As for visual pinball, try a script like this (I'm gonna combine it with the script we've already done.)

Quote
setTitleMatchMode, 2

$Joy1::
send {Joy1}
GetKeyState, state, Joy2
if state = D
send {esc}
return

#IfWinActive, visual pinball

joy3::a

joy4::s

joy5::q

This script will remap joy3 to a, joy 4 to s, and joy5 to q, but only in Visual Pinball. Simply modify to meet your needs. Also, the text that comes after "#IfWinActive," needs to be a string of text from the titlebar of the visual pinball window and it is case sensitive. I have no idea if this is correct (it probably isn't) so you'll need to change that as well.

I'm fairly knowledgeable about AHK, so if any of this is unclear, let me know and I'll try to help some more.

 :cheers:

monkey puzzle

  • Trade Count: (0)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 193
  • Last login:March 23, 2024, 02:11:59 pm
Re: Autohotkey help please.
« Reply #2 on: March 13, 2012, 03:07:01 am »
Wow, thanks for spending the time to help me there.  :notworthy:

I'll give it a try when I get chance and tell you how it goes.

monkey puzzle

  • Trade Count: (0)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 193
  • Last login:March 23, 2024, 02:11:59 pm
Re: Autohotkey help please.
« Reply #3 on: March 14, 2012, 07:40:33 pm »
Just an update. I've tried the 2 buttons = escape configuration and it works.  :applaud:
There was a couple of emulators that this did not work with (one being zsnes) even though exit was mapped to escape in the emulator. This is not the end of the world though, as I can just use different emulators if I need to.

Thanks once again for your help. I will try visual pinball soon.  :cheers:

 

BadMouth

  • Trade Count: (+6)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 9270
  • Last login:July 14, 2025, 01:30:54 pm
  • ...
Re: Autohotkey help please.
« Reply #4 on: March 14, 2012, 10:29:39 pm »
There was a couple of emulators that this did not work with (one being zsnes) even though exit was mapped to escape in the emulator.

Some emulators (such as mame) get input directly and don't recognize the key presses sent by autohotkey.
You can however, still have the autohotkey script exit the application instead of posting esc.
Something like......

Code: [Select]
if state = D
Process, Close, znes.exe
« Last Edit: March 14, 2012, 10:33:50 pm by BadMouth »

monkey puzzle

  • Trade Count: (0)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 193
  • Last login:March 23, 2024, 02:11:59 pm
Re: Autohotkey help please.
« Reply #5 on: March 15, 2012, 06:27:34 pm »

Code: [Select]
if state = D
Process, Close, znes.exe


Thanks Badmouth, this did the job!

Nitz, I tried the script for virtual pinball. When I tried to run Autohotkey it told me there was an error in the line: "Joy3::a" so I changed it to "Joy::send a" which seemed to work, but in virtual pinball when I pressed my flipper button it made the noise for the flipper but oddly the flipper did not move. This was strange because pressing the key on the keyboard made the flipper move and make the sound. Maybe this is something to do with what Badmouth said above about some programs only working with direct key presses.

monkey puzzle

  • Trade Count: (0)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 193
  • Last login:March 23, 2024, 02:11:59 pm
Re: Autohotkey help please.
« Reply #6 on: March 16, 2012, 08:08:04 pm »
Problem solved.

I found that if I use "Joy3::send a" it just sends a quick button press when I hold down the flipper button, this is why I could hear the noise only.
I had to use a more complicated script to keep the a button held down for as long as I held the flipper button down.

Thanks again for everybody that helped me.

nitz

  • Trade Count: (+2)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 507
  • Last login:November 24, 2015, 07:57:29 pm
Re: Autohotkey help please. [solved]
« Reply #7 on: March 17, 2012, 12:53:52 am »
Nitz, I tried the script for virtual pinball. When I tried to run Autohotkey it told me there was an error in the line: "Joy3::a"

Problem solved.

I found that if I use "Joy3::send a" it just sends a quick button press when I hold down the flipper button, this is why I could hear the noise only.
I had to use a more complicated script to keep the a button held down for as long as I held the flipper button down.

Thanks again for everybody that helped me.

Interesting, I confess that I never really messed around with gamepads and ahk so I mistakenly assumed that it worked the same as keys on the keyboard. Good to know that's not the case and glad you got this worked out. :cheers:

monkey puzzle

  • Trade Count: (0)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 193
  • Last login:March 23, 2024, 02:11:59 pm
Re: Autohotkey help please. [one more problem]
« Reply #8 on: March 17, 2012, 07:02:09 pm »

The script I ended up using was this:
Quote
setTitleMatchMode, 2

Joy10::
send {esc}
Process, Close, zsnesw.exe
process, close, fceux.exe
return
2joy10::
send {esc}
Process, Close, zsnesw.exe
process, close, fceux.exe
return

#ifwinactive, Visual Pinball

Joy3::
Send {Lshift down}   ; Hold down the left shift button.
SetTimer, WaitForButtonUp3, 10
return

WaitForButtonUp3:
if GetKeyState("Joy3")  ; The button is still, down, so keep waiting.
    return
; Otherwise, the button has been released.
Send {Lshift up}  ; Release the left shift button.
SetTimer, WaitForButtonUp3, off
return

2Joy3::
Send {rshift down}   ; Hold down the right shift button.
SetTimer, WaitFor2ButtonUp3, 10
return

WaitFor2ButtonUp3:
if GetKeyState("2Joy3")  ; The button is still, down, so keep waiting.
    return
; Otherwise, the button has been released.
Send {rshift up}  ; Release the right shift button.
SetTimer, WaitFor2ButtonUp3, off
return

Joy8::
Send {1 down}   ; Hold down the 1 key.
SetTimer, WaitForButtonUp8, 10
return

WaitForButtonUp8:
if GetKeyState("Joy8")  ; The button is still, down, so keep waiting.
    return
; Otherwise, the button has been released.
Send {1 up}  ; Release the 1 key.
SetTimer, WaitForButtonUp8, off
return

joy7::send 5
2joy7::send 4
return

So when joystick button 10 is inputted (which is actually 2 buttons because the U360 sticks have a shift function) the escape key is activated and also zsnes and fceux emulators are closed.
The rest of the script is to work in visual pinball only. The first 3 parts are for my flipper buttons and start buttons to operate the equivelant key presses for visual pinball and to keep that key pressed while I hold down the button. Then I have the joy7 buttons to operate keys 5 and 4 to add credits.

This all works perfectly. :applaud:
Then I had an idea. I could pull the right joystick back to operate the plunger in Visual Pinball, and the left stick could operate the tilt commands. I searched the help file in AHK and came up with this script which does the job perfectly:
Quote
#Persistent  ; Keep this script running until the user explicitly exits it.
SetTimer, WatchAxis, 5
return

WatchAxis:
GetKeyState, JoyX, JoyX  ; Get position of X axis.
GetKeyState, JoyY, JoyY  ; Get position of Y axis.
GetKeyState, 2JoyX, 2JoyX  ; Get position of X axis.
GetKeyState, 2JoyY, 2JoyY  ; Get position of Y axis.

KeyToHoldDownPrev = %KeyToHoldDown%  ; Prev now holds the key that was down before (if any).

if JoyX = 100
    KeyToHoldDown = /
else if JoyX = 0
    KeyToHoldDown = z
else if 2JoyY = 100
    KeyToHoldDown = Enter
else if JoyY = 100
    KeyToHoldDown = space
else if JoyY = 0
    KeyToHoldDown = t
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
When I run this script on it's own it works perfectly for using the sticks in visual pinball.
My problem is when I combine the 2 scripts together. If I just add the second script onto the end of the first script, then the sticks don't work (In other words only the first part of the script works).
Could somebody help me combine the 2 scripts together please?

nitz

  • Trade Count: (+2)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 507
  • Last login:November 24, 2015, 07:57:29 pm
Re: Autohotkey help please. [one more problem]
« Reply #9 on: March 17, 2012, 07:26:20 pm »
I have neither a gamepad or Visual Pinball handy to test, but try this:

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

setTitleMatchMode, 2

Joy10::
send {esc}
Process, Close, zsnesw.exe
process, close, fceux.exe
return
2joy10::
send {esc}
Process, Close, zsnesw.exe
process, close, fceux.exe
return

#ifwinactive, Visual Pinball

Joy3::
Send {Lshift down}   ; Hold down the left shift button.
SetTimer, WaitForButtonUp3, 10
return

WaitForButtonUp3:
if GetKeyState("Joy3")  ; The button is still, down, so keep waiting.
    return
; Otherwise, the button has been released.
Send {Lshift up}  ; Release the left shift button.
SetTimer, WaitForButtonUp3, off
return

2Joy3::
Send {rshift down}   ; Hold down the right shift button.
SetTimer, WaitFor2ButtonUp3, 10
return

WaitFor2ButtonUp3:
if GetKeyState("2Joy3")  ; The button is still, down, so keep waiting.
    return
; Otherwise, the button has been released.
Send {rshift up}  ; Release the right shift button.
SetTimer, WaitFor2ButtonUp3, off
return

Joy8::
Send {1 down}   ; Hold down the 1 key.
SetTimer, WaitForButtonUp8, 10
return

WaitForButtonUp8:
if GetKeyState("Joy8")  ; The button is still, down, so keep waiting.
    return
; Otherwise, the button has been released.
Send {1 up}  ; Release the 1 key.
SetTimer, WaitForButtonUp8, off
return

joy7::send 5
2joy7::send 4
return

WatchAxis:
GetKeyState, JoyX, JoyX  ; Get position of X axis.
GetKeyState, JoyY, JoyY  ; Get position of Y axis.
GetKeyState, 2JoyX, 2JoyX  ; Get position of X axis.
GetKeyState, 2JoyY, 2JoyY  ; Get position of Y axis.

KeyToHoldDownPrev = %KeyToHoldDown%  ; Prev now holds the key that was down before (if any).

if JoyX = 100
    KeyToHoldDown = /
else if JoyX = 0
    KeyToHoldDown = z
else if 2JoyY = 100
    KeyToHoldDown = Enter
else if JoyY = 100
    KeyToHoldDown = space
else if JoyY = 0
    KeyToHoldDown = t
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

All I did was combine them and then move the first 2 lines of the second script to the top - I think the problem was that the WatchAxis part was never running because of the return statements that appear in the script before it.

drventure

  • Trade Count: (+2)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 4152
  • Last login:April 23, 2024, 06:53:06 pm
  • Laser Death Ray Bargain Bin! Make me an offer!
Re: Autohotkey help please. [one more problem]
« Reply #10 on: March 17, 2012, 08:18:48 pm »
Nitz is right, that first return would prevent anything below it from executing.

Also, you may need another line similiar to this

#ifwinactive, Visual Pinball

but naming a different window, so that if you have multiple windows you're setting keys for, only one part of the script will be active depending on the active window.

monkey puzzle

  • Trade Count: (0)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 193
  • Last login:March 23, 2024, 02:11:59 pm
Re: Autohotkey help please. [one more problem]
« Reply #11 on: March 17, 2012, 09:08:55 pm »
Ok, we are getting somewhere now. I tried Nitz's script and it works. The only problem now is that the part which remaps the joystick movements is active all the time, even when virtual pinball is not running.

Basically I want the first bit of the script (joy10 & 2joy10) to be active all of the time. Everything else in the script is only to be active in Virtual Pinball.

Do I need more #ifwinactive lines? If so, where do I put it/them?

nitz

  • Trade Count: (+2)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 507
  • Last login:November 24, 2015, 07:57:29 pm
Re: Autohotkey help please. [one more problem]
« Reply #12 on: March 17, 2012, 11:29:05 pm »
Again, I have no way to test, but give this a try (the change is in red):

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

setTitleMatchMode, 2

Joy10::
send {esc}
Process, Close, zsnesw.exe
process, close, fceux.exe
return
2joy10::
send {esc}
Process, Close, zsnesw.exe
process, close, fceux.exe
return

#ifwinactive, Visual Pinball

Joy3::
Send {Lshift down}   ; Hold down the left shift button.
SetTimer, WaitForButtonUp3, 10
return

WaitForButtonUp3:
if GetKeyState("Joy3")  ; The button is still, down, so keep waiting.
    return
; Otherwise, the button has been released.
Send {Lshift up}  ; Release the left shift button.
SetTimer, WaitForButtonUp3, off
return

2Joy3::
Send {rshift down}   ; Hold down the right shift button.
SetTimer, WaitFor2ButtonUp3, 10
return

WaitFor2ButtonUp3:
if GetKeyState("2Joy3")  ; The button is still, down, so keep waiting.
    return
; Otherwise, the button has been released.
Send {rshift up}  ; Release the right shift button.
SetTimer, WaitFor2ButtonUp3, off
return

Joy8::
Send {1 down}   ; Hold down the 1 key.
SetTimer, WaitForButtonUp8, 10
return

WaitForButtonUp8:
if GetKeyState("Joy8")  ; The button is still, down, so keep waiting.
    return
; Otherwise, the button has been released.
Send {1 up}  ; Release the 1 key.
SetTimer, WaitForButtonUp8, off
return

joy7::send 5
2joy7::send 4
return

WatchAxis:
IfWinNotActive, Visual Pinball
{
return
}

GetKeyState, JoyX, JoyX  ; Get position of X axis.
GetKeyState, JoyY, JoyY  ; Get position of Y axis.
GetKeyState, 2JoyX, 2JoyX  ; Get position of X axis.
GetKeyState, 2JoyY, 2JoyY  ; Get position of Y axis.

KeyToHoldDownPrev = %KeyToHoldDown%  ; Prev now holds the key that was down before (if any).

if JoyX = 100
    KeyToHoldDown = /
else if JoyX = 0
    KeyToHoldDown = z
else if 2JoyY = 100
    KeyToHoldDown = Enter
else if JoyY = 100
    KeyToHoldDown = space
else if JoyY = 0
    KeyToHoldDown = t
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

monkey puzzle

  • Trade Count: (0)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 193
  • Last login:March 23, 2024, 02:11:59 pm
Re: Autohotkey help please. [one more problem]
« Reply #13 on: March 18, 2012, 08:04:40 am »
 :applaud: Perfect :applaud:

Thanks once again. I would have never had done this without all of you that helped.  :notworthy: