Author Topic: [SOLVED] Autohotkey to trigger exit key in AM?  (Read 26074 times)

rsn8887

  • Full Member
  • ***
  • Posts: 48
    • View Profile
[SOLVED] Autohotkey to trigger exit key in AM?
« on: December 21, 2015, 02:59:40 PM »
I would like to use the exit key functionality from Attract Mode to be able to exit any emulator quickly. But I want to only exit if I hold "Player 1 Joy Button 7" and THEN press "Player 1 Joy Button 8".

Since AM does not support such a combo, my idea is to configure "Esc" as exit key in Attract Mode for all my emulators. Then I planned to use Autohotkey to send the "Esc" keycode to Attract Mode whenever I hold Joy 7 and then press Joy 8 (or any other key, I also tried this with some other non-Escape keys).

Separately, everything works:

- I got Autohotkey to work (testing with Notepad) by itself and it can send any keypress I wish when I press Joy 8 while holding down Joy 7.

-The exit key in Attract Mode works by itself. Pressing escape closes any emulator.

But when I put it together, and try my Joy 7 & Joy 8 hotkey inside an emulator started by AM, nothing happens.

Here's what's weird: if I set Autohotkey to send "Alt F4" on my Joy 7 & Joy 8 it works in almost all emulators.

So somehow Attract Mode is not accepting the escape keycode whenever it is send by Autohotkey, but it reacts perfectly fine if I manually press key.

Has anybody figured this out and could help me?

I tried a few things such as that #installkeyboardhook command in AHK and some other things, but have had no luck.

Thank you!
« Last Edit: December 22, 2015, 11:19:39 AM by rsn8887 »

Namachieli

  • Full Member
  • ***
  • Posts: 33
    • View Profile
Re: Autohotkey to trigger exit key in AM?
« Reply #1 on: December 21, 2015, 08:35:37 PM »
I'm not super great with AHK, but IIRC there is a distinction between sending a simulated key press and sending a key input.

HTH
Neon 18.04 x64. i5 4690k, 16 GB RAM, EVGA GTX 750ti, DS3 SIXAXIS via BT/USB, RetroArch/libretro.

Namachieli

  • Full Member
  • ***
  • Posts: 33
    • View Profile
Re: Autohotkey to trigger exit key in AM?
« Reply #2 on: December 22, 2015, 08:48:34 AM »
Spent some more time looking at this and thinking about it.

Quote
Separately, everything works:

So thinking about this, it make sense in a weird way.

AHK sends the keypress to the system when you hit the button combo, you can see it in a text editor because it's being all it cares about is the input.

Setting it to Alt-f4 closes the emulator for the same reason, the system receives the input and acts.

The reason mapping it to esc doesn't work is because the emulator is not looking for the system receive an esc input, but rather for the key to be pressed.

https://autohotkey.com/board/topic/62117-implementation-of-send-vs-controlsend/

Can you post your script?
« Last Edit: December 22, 2015, 08:52:11 AM by Namachieli »
Neon 18.04 x64. i5 4690k, 16 GB RAM, EVGA GTX 750ti, DS3 SIXAXIS via BT/USB, RetroArch/libretro.

rsn8887

  • Full Member
  • ***
  • Posts: 48
    • View Profile
Re: Autohotkey to trigger exit key in AM?
« Reply #3 on: December 22, 2015, 09:29:20 AM »
My script is this:

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.

SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.

SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.
#SingleInstance force

;#InstallKeybdHook

#If ( not WinActive("Attract-Mode") )
Joy7::
If GetKeyState("Joy8")
{
SetKeyDelay, 10, 10
;   Send !{F4}
   Send {Escape}
;ControlSend,,1,Attract-Mode
}
Return
#If


I tried various things, such as ControlSend, sending !{F4} whenever Attract Mode is NOT active (to prevent the user from killing AM), etc. Nothing worked.
« Last Edit: December 22, 2015, 09:30:56 AM by rsn8887 »

rsn8887

  • Full Member
  • ***
  • Posts: 48
    • View Profile
Re: Autohotkey to trigger exit key in AM?
« Reply #4 on: December 22, 2015, 10:44:00 AM »
I think I understand it now. It is like you said.

AM looks for the exit hotkey using the GetAsyncKeyState method. This bypasses the event handling usually associated with keyboard input and allows AM to always react in case the user presses the exit key, even though it is in the background. Autohotkey, AFAIK, cannot "simulate" such an event that would be registered by GetAsyncKeyState. When autohotkey sends events, it uses other methods.


Namachieli

  • Full Member
  • ***
  • Posts: 33
    • View Profile
Re: Autohotkey to trigger exit key in AM?
« Reply #5 on: December 22, 2015, 10:59:14 AM »
Quote
I think I understand it now

Glad to hear. If you get it working properly, post it in this thread incase someone else needs to do something similar. :)
Neon 18.04 x64. i5 4690k, 16 GB RAM, EVGA GTX 750ti, DS3 SIXAXIS via BT/USB, RetroArch/libretro.

rsn8887

  • Full Member
  • ***
  • Posts: 48
    • View Profile
Re: Autohotkey to trigger exit key in AM?
« Reply #6 on: December 22, 2015, 11:19:25 AM »
Nevermind I just got it to work. Here is the working script. After looking at AM source I saw that it checks for the exit key every 100 ms or so. I figured maybe AHK was sending the key press for much too short.

So I disabled the line Sendmode Input at the top. This forces it into Sendmode Event, which allows for selectable key duration. I then used SetKeyDelay to set the duration to 110 ms, and now it works!

Contents of "CabScript.ahk":

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.

SendMode Event ; ensure compatibility with Attract Mode exit key

SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.
#SingleInstance force

#If ( not WinActive("Attract-Mode") )
Joy7::
If GetKeyState("Joy8")
{
   SetKeyDelay, -1, 110
   Send {Escape} ;set exit key in Attract Mode to escape for this to work
}
#If
Return


I can now quit all emulators using the joystick combo "hold button 7 then press button 8." It will not trigger any other way. I put in an if statement so it will not do anything if attract mode is showing its menu.

Note that some emulators such as mame will NOT respond to AHK macros at all. Doing the exit this way ensures that it works even with mame.
« Last Edit: December 22, 2015, 11:30:29 AM by rsn8887 »

xbs

  • Sr. Member
  • ****
  • Posts: 180
    • View Profile
Re: [SOLVED] Autohotkey to trigger exit key in AM?
« Reply #7 on: December 22, 2015, 01:36:08 PM »
You can set key combos in MAME to exit.

rsn8887

  • Full Member
  • ***
  • Posts: 48
    • View Profile
Re: [SOLVED] Autohotkey to trigger exit key in AM?
« Reply #8 on: December 22, 2015, 05:01:24 PM »
Yes, but not this way. I want it to only trigger if Joy 7 is held then Joy 8 is pressed. It should not trigger if Joy 8 is pressed first and then Joy 7.

AFAIK mame only allows AND (pressed simultaneously), OR combinations. So it would trigger any time both those buttons are pressed. Also the key binding in mame is a royal pain (but that is another topic).

Also, I don't want to configure every emulator's exit key in the emulators themselves.

rsn8887

  • Full Member
  • ***
  • Posts: 48
    • View Profile
Re: [SOLVED] Autohotkey to trigger exit key in AM?
« Reply #9 on: December 22, 2015, 05:07:15 PM »
Is there any documentation on how the key binding menu in MAME works? How do I erase a key binding. How do I go back to default?

EDIT: I found out by testing all kinds of keys in Mame to bind. Delete toggles between "none" and default key. Pressing Enter then a key binds that key. Pressing Enter and another key binds with OR. Pressing enter, then one key and quickly a second key binds AND. Pressing one key, then another key twice quickly binds NOT. Not very intuitive, and not all of this is documented anywhere. Mame does not have a documentation.

Those are useful, but I still prefer to use AM exit key functionality because I can program other combinations with AHK.
« Last Edit: December 22, 2015, 06:18:29 PM by rsn8887 »

xbs

  • Sr. Member
  • ****
  • Posts: 180
    • View Profile
Re: [SOLVED] Autohotkey to trigger exit key in AM?
« Reply #10 on: December 23, 2015, 02:39:07 AM »
You're right, mame can only do combos and not sequence button pushes.

I only use mame... but for other emus your solution is way better one.

verion

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 863
    • View Profile
    • new projects
Re: [SOLVED] Autohotkey to trigger exit key in AM?
« Reply #11 on: December 23, 2015, 02:43:38 AM »
Nice finding.

After erasing JOY1 UP I couldn't bind it again. I've end up reseting (erasing) mame key config.
To reset MAME key bindings to default you just have to delete /cfg/default.cfg file in your MAME directory/folder
You can also edit your key bindings opening this file in text editor.

akafox

  • Hero Member
  • *****
  • Posts: 985
    • View Profile
Re: [SOLVED] Autohotkey to trigger exit key in AM?
« Reply #12 on: December 23, 2015, 08:02:07 AM »
to erase a key binding in mame you can delete the "default.cfg" in mame/cfg as verion said

or (how I do it) highlight the "messed-up" key settings re highlight it and it will allow you to do new key bindings..

also pressing the escape key as a key after moving it back will set it to "none"

depending on the emulators you are using you might be able to manually edit the key bindings...just a though...

People want life easy..then complain about it

rsn8887

  • Full Member
  • ***
  • Posts: 48
    • View Profile
Re: [SOLVED] Autohotkey to trigger exit key in AM?
« Reply #13 on: December 23, 2015, 10:33:25 AM »
Ok I ran into a bunch more problems configuring all my exit keys. I realized that I want to quit each emulator "cleanly" so it has time to save its config, delete temp files etc.

For example, Retroarch (sic!) extracts zip files for roms, but only deletes the last extracted file on a clean exit. So if I use the exit key, which kills retroarch, it leaves all those files behind.

I found that most emulators exit cleanly on "escape." Kega Fusion required the -fullscreen argument to force it to quit on escape instead of switching to windowed mode. Snes9x and Nestopia show a menu on escape. I could not change that behaviour. What were the authors smoking? I am considering switching all those over to retroarch to get some more sane behavior, if only retroarch wouldn't be such a usability trainwreck.

So I now chose an autohotkey script that sends "escape" to the emulator first, waits 2 seconds, and if we are not back in Attract Mode, it sends the exit key to force kill the emulator. I then chose a neutral exit key, I settled on "divide" on the numpad.

Here is my new script content.

Contents of "ArcadeCabinetHotkeys.ahk":

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.

; #Warn  ; Enable warnings to assist with detecting common errors.

SendMode Event ; For compatibility with Attract Mode

SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.
#SingleInstance force


#If ( not WinActive("Attract-Mode") )
Joy7::
If GetKeyState("Joy8")
{
   SetKeyDelay, -1, 110 ; Need to press exit key longer than 100 ms, because Attract Mode only checks it every 100 ms
   Send {Escape} ; Most Emulators quit with escape key so try this first
   Sleep, 2000 ; wait a second
   If ( not WinActive("Attract-Mode") ) ; are we back in Attract Mode yet?
   {
       Send {NumpadDiv} ; No: use Attract Mode exit key NumpadDiv
   }     
}
Return
#If
« Last Edit: December 23, 2015, 10:35:24 AM by rsn8887 »

rsn8887

  • Full Member
  • ***
  • Posts: 48
    • View Profile
Re: [SOLVED] Autohotkey to trigger exit key in AM?
« Reply #14 on: December 23, 2015, 10:53:43 AM »
Ok Nestopia allows to remap that insane "Escape for menu" behaviour. So I mapped escape to clean exit, and used Tab to show the menu in Nestopia.

Then I saw that snes9x default exit key is Alt-F4 anyways. So with that change my script should now exit all emulators cleanly apart from Mame.