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 - RESOLVED!!!!!!  (Read 14706 times)

0 Members and 1 Guest are viewing this topic.

severdhed

  • Trade Count: (0)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 2971
  • Last login:October 07, 2022, 04:45:32 pm
  • RIP Dinosaur Hippo
AutoHotkey help please - RESOLVED!!!!!!
« on: October 07, 2010, 01:32:03 pm »
i need some help with an autohotkey script.  i am having two issues really...

1. I need help adjusting this script i found to change the functionality of it
2. i can't seem to get the script to run automatically upon boot.

1. here is my goal. i have a single button on my panel that i would like to function as both pause and exit.   ideally, i would like it so that if i press it quickly, it sends one keystroke, and if i hold it for 2 or 3 seconds, it sends a different keystroke.  i found a script on another thread(dont remember where) that handles the long press...it was setup for the esc key, however i modified because the button i want to use is connected to the "5" key on my keywiz.  it works fine...if i press the button quickly, nothign happens, if i hold it in for a few seconds, it sends a "5" key.  i've played around and got it to send a different keystroke instead of 5, so i think it can be done, i just dont know enough how to get it to work properly every time.  right now, mame is set to exit with the "5" key and pause with the "9" key.  so i'd like the script setup so that when i press the 5 key quickly, it sends a 9, but if i hold it down, it sends a 5.

can anyone help me?  below is the script that i am using now for the long press.

2. i am running windows xp pro on my cabinet, with Mala.exe as the windows shell.  i've tried placing a shortcut to the .ahk file in the startup folder, but that doesn't seem to be processed until explorer launches.  i then tried compiling the script into an executable file and placed it in the registry under : HKLM\software\microsoft\windows\currentversion\run  but that still doesnt seem to load upon bootup.  if i exit mala, explorer launches and then the script starts running...at that point if i go back into mala, everything works fine and my delayed press is working.  my question is how do i get this script to run automatically at boot when using mala as the shell?  i can't be the only one trying to do this.  my only thought at this point would be to create a batch file that launches the ahk script, and then launches mala, and set that batch file as the shell, but i dont know if that would work.

any ideas?


EDIT: the problem with getting it to run automatically at boot has been solved, the script has to be compiled as an executable and then set up to run as a service within windows. i used a program called srvany to accomplish this and it works great.  here are the instructions i followed in case it helps anyone else in the future..

http://www.tacktech.com/display.cfm?ttid=197
« Last Edit: October 13, 2010, 09:59:20 pm by severdhed »
Current Projects:      Zak-Man | TMNT Pedestal | SNES Pi | N64 Odroid
Former Projects:     4 Player Showcase | Donkey Kong | iCade

severdhed

  • Trade Count: (0)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 2971
  • Last login:October 07, 2022, 04:45:32 pm
  • RIP Dinosaur Hippo
Re: AutoHotkey help please
« Reply #1 on: October 07, 2010, 05:16:51 pm »
i forgot to put the script in my first post...



;HoldEscape:

toggletimer = 0
esckill = 1
SetKeyDelay, 10, 20

$5::
   if toggletimer = 0
      SetTimer, resettimer, 1000
   toggletimer++
   if toggletimer > 3
      goto, toggleesckill
   if esckill = 1
   {
      tt=0 ; set time to 0
      loop,100
      {
         GetKeyState, s , 5, p
        if s=U
           Break
         tt+=25
         sleep 25
      }
      if tt>=2000  ; if key pressed for 2 or more sec
      {
         send, {5}
         toggletimer = 0
         sleep 1200
      }
   }
   else
   {
      send, {5}
   }
return

resettimer:
   toggletimer = 0
return

toggleesckill:
   soundplay, *48
   toggletimer = 0
   esckill *= -1
   sleep 500
return

Current Projects:      Zak-Man | TMNT Pedestal | SNES Pi | N64 Odroid
Former Projects:     4 Player Showcase | Donkey Kong | iCade

nitz

  • Trade Count: (+2)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 507
  • Last login:November 24, 2015, 07:57:29 pm
Re: AutoHotkey help please
« Reply #2 on: October 08, 2010, 02:45:06 am »
Hey. Saw your post related to this in the MaLa forum - was wondering what happened to you on that thread. ;) Figure it makes more sense to respond to this one.

I think what you want is possible - don't know off the top of my head and it's bedtime for me. Will think about it though. Been using AHK a lot lately and I always like figuring out/learning new stuff, so I'll see what I can come up with.

Oh yeah, and the problem with running it on startup is that when you're using something else as a shell, you have to run any startup programs as a service. Check the wiki, I think there might be some links there, or you could just google start program as service or something like that. Alternatively, you can just go back to using explorer as the shell and then putting the script in your startup folder should work. Having a front end as a shell is neat for sure, but you can get almost the same effect just by clearing your desktop of icons and turning it all black, and then you don't have hassles like this. Once the FE is running, it's all the same anyway.

severdhed

  • Trade Count: (0)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 2971
  • Last login:October 07, 2022, 04:45:32 pm
  • RIP Dinosaur Hippo
Re: AutoHotkey help please
« Reply #3 on: October 08, 2010, 09:21:01 am »
awesome.  i was lying in bed last night thinking about this, i was thinking about all the possible ways to start a program automatically and which ones might work.  then i remembered that VNC was working fine, and it runs as a service, so my plan was to look at ways to run a program as a service when i got time today, and then i wake up and see this post.  that makes me feel good that i was at least on the right track.  i'll give it a try this evening when i get home from work.

thanks
Current Projects:      Zak-Man | TMNT Pedestal | SNES Pi | N64 Odroid
Former Projects:     4 Player Showcase | Donkey Kong | iCade

severdhed

  • Trade Count: (0)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 2971
  • Last login:October 07, 2022, 04:45:32 pm
  • RIP Dinosaur Hippo
Re: AutoHotkey help please
« Reply #4 on: October 08, 2010, 04:46:28 pm »
ok, i just got home from work, and used srvany to setup my ahk script as a system service and it seems to be working fine.  now i just need to figure out how to get it to send an alternate keystroke with a quick press.  thanks for all the help so far.
Current Projects:      Zak-Man | TMNT Pedestal | SNES Pi | N64 Odroid
Former Projects:     4 Player Showcase | Donkey Kong | iCade

nitz

  • Trade Count: (+2)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 507
  • Last login:November 24, 2015, 07:57:29 pm
Re: AutoHotkey help please
« Reply #5 on: October 08, 2010, 08:51:14 pm »
This works!

;HoldEscape:

toggletimer = 0
esckill = 1
SetKeyDelay, 10, 20

$5::
sleep 200 ;may need to adjust this depending on your computer. 200 works for me, but 100 was unreliable
GetKeyState, state, 5
if state = U
{
send {9}
}
   if toggletimer = 0
      SetTimer, resettimer, 1000
   toggletimer++
   if toggletimer > 3
      goto, toggleesckill
   if esckill = 1
   {
      tt=0 ; set time to 0
      loop,100
      {
         GetKeyState, s , 5, p
        if s=U
           Break
         tt+=25
         sleep 25
      }
      if tt>=2000  ; if key pressed for 2 or more sec
      {
         send, {5}
         toggletimer = 0
         sleep 1200
      }
   }
   else
   {
      send, {5}
   }
return

resettimer:
   toggletimer = 0
return

toggleesckill:
   soundplay, *48
   toggletimer = 0
   esckill *= -1
   sleep 500
return

------------------------

A couple notes:

When you hold down 5 to exit, it will pause first. I'm thinking there may not be a way around that. So I added in a line to unpause before it exits in case you're using autosave as it would be annoying to have games load up and be paused.

The other note is, if you rapidly press pause a few times, it will exit. I don't see why anyone would do that though.

EDIT: Heh heh, the way around that problem crossed out above was actually pretty simple. I made the necessary adjustment to the script.
« Last Edit: October 08, 2010, 09:18:30 pm by nitz »

wxforecaster

  • Trade Count: (+2)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 105
  • Last login:February 28, 2019, 01:19:24 pm
Re: AutoHotkey help please
« Reply #6 on: October 08, 2010, 11:47:43 pm »
This is great work, and VERY timely. I was also wanting to put a single pause/exit button on my cab (currently in design) using this exact "push vs. hold" methodology. I'm a computer programmer by trade and this AutoHotKey script is really fantastic stuff.

Nice work!! Going to see if there are any "loop holes" in it

Evan

severdhed

  • Trade Count: (0)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 2971
  • Last login:October 07, 2022, 04:45:32 pm
  • RIP Dinosaur Hippo
Re: AutoHotkey help please
« Reply #7 on: October 09, 2010, 10:31:08 am »
the script is pretty awesome, and i appreciate all the help.  i am however having some problems with it.  sometimes it works perfectly, other times it won't pause, or won't unpause, then maybe i'll hit it 3 or 4 times and it will pause/unpause again. and sometimes i cant get it to exit.  i'm not sure what is causing it, but it just sometimes doensn't work.  any ideas?
Current Projects:      Zak-Man | TMNT Pedestal | SNES Pi | N64 Odroid
Former Projects:     4 Player Showcase | Donkey Kong | iCade

nitz

  • Trade Count: (+2)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 507
  • Last login:November 24, 2015, 07:57:29 pm
Re: AutoHotkey help please
« Reply #8 on: October 09, 2010, 06:38:46 pm »
I'll admit I only tested this in notepad as I didn't have mame handy. It seemed to work just fine in notepad. Sometimes mame can get a little picky about how keypresses are sent to it and so I'm guessing that's the problem. Try this and see if it works better.

;HoldEscape:

toggletimer = 0
esckill = 1
SetKeyDelay, 10, 20

$5::
sleep 200 ;may need to adjust this depending on your computer. 200 works for me, but 100 was unreliable
GetKeyState, state, 5
if state = U
{
send {9 downtemp}
send {9 up}
}
   if toggletimer = 0
      SetTimer, resettimer, 1000
   toggletimer++
   if toggletimer > 3
      goto, toggleesckill
   if esckill = 1
   {
      tt=0 ; set time to 0
      loop,100
      {
         GetKeyState, s , 5, p
        if s=U
           Break
         tt+=25
         sleep 25
      }
      if tt>=2000  ; if key pressed for 2 or more sec
      {
         send {5 downtemp}
         send {5 up}
         toggletimer = 0
         sleep 1200
      }
   }
   else
   {
      send {5 downtemp}
      send {5 up}
   }
return

resettimer:
   toggletimer = 0
return

toggleesckill:
   soundplay, *48
   toggletimer = 0
   esckill *= -1
   sleep 500
return

severdhed

  • Trade Count: (0)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 2971
  • Last login:October 07, 2022, 04:45:32 pm
  • RIP Dinosaur Hippo
Re: AutoHotkey help please
« Reply #9 on: October 09, 2010, 10:19:51 pm »
it still isn't working quite right in mame.  seems to work as expected in notepad, but in mame, it is inconsistent...any ideas?  it seems to pause when i try, but often when i try to unpause it, i have to try several times before it works.  also sometimes when i press and hold it, it pauses right away, but will not exit, even after holding for 10 seconds.  that after a few retries, it works.  it isn't that big of a deal, i can work around it, but ideally i want this so that other people can play it without dealing with the weird little inconsistencies.

i really appreciate all the help so far, and if it can't be done, thats fine..it just seems like it is so close, it seems like it is feasible.  i just wish i understood this ahk program better.  i guess i'm going to have to look for some tutorials or something.
Current Projects:      Zak-Man | TMNT Pedestal | SNES Pi | N64 Odroid
Former Projects:     4 Player Showcase | Donkey Kong | iCade

wxforecaster

  • Trade Count: (+2)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 105
  • Last login:February 28, 2019, 01:19:24 pm
Re: AutoHotkey help please
« Reply #10 on: October 10, 2010, 12:37:59 am »
It's not surprising this code isn't working (and yet appears working in Notepad). After spending an hour learning the AutoHotKey syntax, I see there are sections of code stepping on one another, such as this resettimer function which basically creates an infinite loop.

I'm working on an overhaul to this script.

Evan

wxforecaster

  • Trade Count: (+2)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 105
  • Last login:February 28, 2019, 01:19:24 pm
Re: AutoHotkey help please
« Reply #11 on: October 10, 2010, 02:05:06 am »
EDIT: THIS IS A LONG THREAD. SEE FINAL WORKING CODE NEAR END OF THIS PAGE


This should be much much simpler, assuming that the keyboard encoder completely mimics a keyboard.
Basically when you press the "5" key (or corresponding CP button), I start a timer. The program waits until you release the 5 key. If the time elapsed is greater than some user-defined number (e.g. 2 seconds), it will send the exit key. If it's less, it sends the pause/unpause key.

/*
AUTHOR: WXFORECASTER 10/9/10

DESCRIPTION: DESIGN OVERRIDE SUCH THAT A SINGLE KEY PRESS AND KEY HOLD
             GENERATE DIFFERENT FUNCTIONALITY

IF WE PRESS 5 KEY = SEND 9 (MAME PAUSE KEY)
IF WE HOLD 5 KEY FOR X SECONDS = SEND 5 (MAME EXIT KEY)
*/




/*
BEGIN HOLDESCAPE CODE
SET USER DEFINED VARIABLES BELOW
*/
timeToExit := 2000 ;time in milliseconds (e.g. 2000ms = 2 seconds) key must be pressed before sending exit


;Initialize our timer
timeElapsed := 0

/*
USE $5 as hook such that sending the 5 key to exit MAME won't force this script into a circular recursive loop
*/
$5::

;Set a timer (checkTimer) in the background, which every 200ms keeps a tally of the total timer count
SetTimer, checkTimer, 200

KeyWait,5,T3  ; Wait for the 5 key to be released with a 3 second timeout (T3)
SetTimer, checkTimer, Off ;Turn Timer Off

if (timeElapsed >= timeToExit)
{
   send {Blind}{5 downtemp} ;Send MAME Exit
   send {Blind}{5 up}

}
else
{
   send {Blind}{9 downtemp} ;Send MAME pause/unpause
   send {Blind}{9 up}
}        
timeElapsed := 0 ;Reset timer to zero
return


checkTimer:
   timeElapsed := timeElapsed + 200
return
« Last Edit: October 16, 2010, 11:10:31 am by wxforecaster »

brian_hoffman

  • Trade Count: (0)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 131
  • Last login:July 02, 2011, 09:02:20 pm
Re: AutoHotkey help please
« Reply #12 on: October 10, 2010, 12:33:55 pm »
Your code will never work in mame until you do the following.
Download MAME SOURCE and Headkazes custom compilier. Download and PATCH using the correct NONAG PATCH (This step not needed really)
Navigate to your .\mame0139s\src\osd\windows\input.c (edit the file in PSPAD or NOTEPAD)

Look for
// For testing purposes: force DirectInput
#define FORCE_DIRECTINPUT   0

Change it to
// For testing purposes: force DirectInput
#define FORCE_DIRECTINPUT   1

Then Compile MAME. Now you should be able to send keys to MAME.

Credit of this goes to Headkaze and I have been using this for a while because I do not like how Mame separates multiple keyboards and give me added flexibility.

severdhed

  • Trade Count: (0)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 2971
  • Last login:October 07, 2022, 04:45:32 pm
  • RIP Dinosaur Hippo
Re: AutoHotkey help please
« Reply #13 on: October 10, 2010, 01:20:29 pm »
i already recompiled mame using instructions supplied by nitz in this thread...

http://forum.arcadecontrols.com/index.php?topic=105538.0

i didn't make that exact change, but this is what he told me to do:


4. Now here's the part where we fix it so autohotkey will work again. Navigate your way to C:\mamesrc\src\osd\windows\. Open the file input.c. If you double-click it, it should just open in notepad, if not, open it with notepad. Now, find the following text:

// initialize RawInput and DirectInput (RawInput first so we can fall back)
rawinput_init(machine);
dinput_init(machine);
win32_init(machine);

and change it to this:

// initialize RawInput and DirectInput (DirectInput first so we can fall back)
dinput_init(machine);
rawinput_init(machine);
win32_init(machine);

Close the file and save your changes. We're basically just making mame revert to its old input system. I'm not sure if there are any negative side effects to this - I've never seen any, but this info is provided without warranty of any kind.


it appears as if the change to mame.exe is working. before i did that, it didn't recognize the inputs from the script, now it does.

i am going to test this new script right now, i'll report back later
Current Projects:      Zak-Man | TMNT Pedestal | SNES Pi | N64 Odroid
Former Projects:     4 Player Showcase | Donkey Kong | iCade

severdhed

  • Trade Count: (0)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 2971
  • Last login:October 07, 2022, 04:45:32 pm
  • RIP Dinosaur Hippo
Re: AutoHotkey help please
« Reply #14 on: October 10, 2010, 01:24:49 pm »
i tried that script, but it seems to be incomplete.  when i run it, it throws an error about a missing "}".  see attached pic:

Current Projects:      Zak-Man | TMNT Pedestal | SNES Pi | N64 Odroid
Former Projects:     4 Player Showcase | Donkey Kong | iCade

wxforecaster

  • Trade Count: (+2)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 105
  • Last login:February 28, 2019, 01:19:24 pm
Re: AutoHotkey help please
« Reply #15 on: October 10, 2010, 02:38:04 pm »
Severdhed,


My apologies. Stripping out all of my test code I inadvertantly removed the closing } bracket on the if comment.
I've modified my post above with the correct code.

I'm personally using HyperSpin which allows you to launch an AHK script, with emulator-specific coding. While I have not yet tried my code inside a MAME setting, I'm not apparently on the up and up as to why that would be necessary with the recompile, etc... Therefore, I again offer the caveat that my code logic works in a standard Windows setting.

Evan
« Last Edit: October 10, 2010, 10:28:00 pm by wxforecaster »

nitz

  • Trade Count: (+2)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 507
  • Last login:November 24, 2015, 07:57:29 pm
Re: AutoHotkey help please
« Reply #16 on: October 10, 2010, 03:51:09 pm »
Shoot, in my last post, I changed commands like send {5} to send {5 downtemp} send {5 up} but forgot that it's actually send {Blind} {5 downtemp} send {Blind} {5 up}. Thanks for the reminder wxforecaster!

Try making that adjustment, just inserting {Blind} after send everytime and see if that works.

I know mame and ahk can play nice together. I remember I had some trouble getting stuff like this working the first time too. But it can be done. We'll figure this out guys!

wxforecaster

  • Trade Count: (+2)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 105
  • Last login:February 28, 2019, 01:19:24 pm
Re: AutoHotkey help please
« Reply #17 on: October 10, 2010, 04:11:25 pm »
Nitz,

What is the reasoning behind the {Blind} send and the downtemp/up combo? It seems this would be related to capslock, shift, control, and alt based on the AHK documentation, and should not be applicable in this case.

I'm not seeing any downsides to doing this, just not understanding the reasoning.

Evan


When {Blind} is the first item in the string, the program avoids releasing Alt/Control/Shift/Win if they started out in the down position. For example, the hotkey +s::Send {Blind}abc would send ABC rather than abc because the user is holding down the Shift key.

"The word DownTemp may also be used. Its effect is the same as Down except for the modifer keys (Control/Shift/Alt/Win). In those cases, DownTemp tells subsequent sends that the key is not permanently down, and may be released whenever a keystroke calls for it. For example, Send {Control DownTemp} followed later by Send a would produce a normal "a" keystroke, not a control-A keystroke."

severdhed

  • Trade Count: (0)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 2971
  • Last login:October 07, 2022, 04:45:32 pm
  • RIP Dinosaur Hippo
Re: AutoHotkey help please
« Reply #18 on: October 10, 2010, 05:27:19 pm »
ok, wxforecaster, i tried your script, it works well in notepad, however it doesnt seem to work at all in mame....nothign happens at all when i press that button (or the 5key on my keyboard)...im not sure what is different.
Current Projects:      Zak-Man | TMNT Pedestal | SNES Pi | N64 Odroid
Former Projects:     4 Player Showcase | Donkey Kong | iCade

wxforecaster

  • Trade Count: (+2)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 105
  • Last login:February 28, 2019, 01:19:24 pm
Re: AutoHotkey help please
« Reply #19 on: October 10, 2010, 06:47:56 pm »
severdhed,

waiting for Nitz to reply regarding his code. You might want to try that there.

In my code, replace Send {5} with:

send {Blind} {5 downtemp}
send {Blind} {5 up}

and replace Send {9} with:

send {Blind} {9 downtemp}
send {Blind} {9 up}


nitz

  • Trade Count: (+2)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 507
  • Last login:November 24, 2015, 07:57:29 pm
Re: AutoHotkey help please
« Reply #20 on: October 10, 2010, 08:57:59 pm »
Nitz,

What is the reasoning behind the {Blind} send and the downtemp/up combo? It seems this would be related to capslock, shift, control, and alt based on the AHK documentation, and should not be applicable in this case.

I'm not seeing any downsides to doing this, just not understanding the reasoning.

Evan

Good question - and to be perfectly honest, I don't know what the reasoning is. I read about it somewhere awhile back (I'm pretty sure it was the ahk forum) as a possible solution to sending keypresses to apps that seem to be a bit wonky when it comes to accepting keypresses. I tried it with mame and it worked great for me. I would love to try your script with mame right now, but I'm pretty busy tonight and don't have a suitable mame install handy. Would like to hear from severdhed if he was able to get it to work using {Blind}.

severdhed

  • Trade Count: (0)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 2971
  • Last login:October 07, 2022, 04:45:32 pm
  • RIP Dinosaur Hippo
Re: AutoHotkey help please
« Reply #21 on: October 10, 2010, 09:38:32 pm »
that seemed to do the trick.  it is weird, because in notepad, that sends a space before and after the desired key, but it seems to work well with mame.  i did have one time that it failed to unpause, but i then tried it about a dozen or so times and it worked great. 

thank you guys for all of your work, now i won't have to yell at my almost 2 year old son who loves to press that exit button.

on a side note i have one more question...

it seems to take nearly 5 to 6 seconds of holding the button down until it exits, even though it looks like it is set for 2000ms, what is causing the extra delay?  it isn't that big of a deal, and is very usable this way, it just seems a little longer than i expected.
Current Projects:      Zak-Man | TMNT Pedestal | SNES Pi | N64 Odroid
Former Projects:     4 Player Showcase | Donkey Kong | iCade

wxforecaster

  • Trade Count: (+2)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 105
  • Last login:February 28, 2019, 01:19:24 pm
Re: AutoHotkey help please
« Reply #22 on: October 10, 2010, 10:17:20 pm »
Severhed,

What's likely happening is that the button is timing out (set to 5 seconds) because you're not letting go of it.

As I wrote the code, it triggers when you release the button (or times out after 5 seconds).

Basically the following:
1.) Let go of key press < 2 seconds = pause/unpause
2.) Let go of key press after 2 seconds = exit mame
3.) Never let go. Code times out after 5 seconds = exit mame.

Basically, hold it in at least 2 seconds and let go and it will exit.

If you wish to have it exit while still holding it down, then that will require a code modification.
In the meantime, you could set the timeout on the KeyWait from 5 to 3 seconds.
KeyWait,5,T3

To get rid of the extraneous "space", get rid of the space after the {Blind} tags so the brackets abutt one another.

I modified my code above t oaccount for these changes.

Evan
« Last Edit: October 10, 2010, 10:24:17 pm by wxforecaster »

severdhed

  • Trade Count: (0)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 2971
  • Last login:October 07, 2022, 04:45:32 pm
  • RIP Dinosaur Hippo
Re: AutoHotkey help please
« Reply #23 on: October 10, 2010, 10:47:13 pm »
ah..that makes sense.  i've been looking through the scrips, the notes are very helpful.  how did you learn how to use this?  is there good documentation somewhere?

i changed the timeout to 3 and it is working great, i couldn't be happier with it now. i am using a translucent light up button for this function and now i have to decided if i want to label it Pause or Exit.


while we are at it, any chance you could help me clean up my just plain 2 second exit script?  I have two cabinets...the one we have been working on is for my showcase cabinet where i only have 1 button for both functions.  on my vertical classics cabinet, i have dedicated pause and exit buttons.  even though the kids don't play that cabinet as much, i still think it would benefit from a delay on the exit button.  (the cabinet sits next to a set of stairs, my kids can reach over the hand rail and hit the exit button.(right in the middle of an epic millipede game)

i could maybe figure it out by cutting part of the code from your script, but i figured i'd probably be better off to let the pro's handle this.  here is the script i am using now.  it seems to work fine, but seeing the problems that i ran into the with the other one, i figured cleaning it up a bit might be a good idea.


;HoldEscape:

toggletimer = 0
esckill = 1
SetKeyDelay, 10, 20

$n::
   if toggletimer = 0
      SetTimer, resettimer, 1000
   toggletimer++
   if toggletimer > 3
      goto, toggleesckill
   if esckill = 1
   {
      tt=0 ; set time to 0
      loop,100
      {
         GetKeyState, s , n, p
        if s=U
           Break
         tt+=25
         sleep 25
      }
      if tt>=2000  ; if key pressed for 2 or more sec
      {
         send, {n}
         toggletimer = 0
         sleep 1200
      }
   }
   else
   {
      send, {n}
   }
return

resettimer:
   toggletimer = 0
return

toggleesckill:
   soundplay, *48
   toggletimer = 0
   esckill *= -1
   sleep 500
return


____________________________


i am using the "n" key as the exit button on this cabinet.  thanks for your help...this is so awesome.
Current Projects:      Zak-Man | TMNT Pedestal | SNES Pi | N64 Odroid
Former Projects:     4 Player Showcase | Donkey Kong | iCade

severdhed

  • Trade Count: (0)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 2971
  • Last login:October 07, 2022, 04:45:32 pm
  • RIP Dinosaur Hippo
Re: AutoHotkey help please
« Reply #24 on: October 10, 2010, 11:15:28 pm »
also, i used the modified code to remove the spaces, but it seems to be less reliable when used that way for some reason.  with the spaces in there, it seems to work every time (in the few minutes i was able to test it) however once the spaces were removed, it did not seem to work very consistently at all...not sure why though
Current Projects:      Zak-Man | TMNT Pedestal | SNES Pi | N64 Odroid
Former Projects:     4 Player Showcase | Donkey Kong | iCade

wxforecaster

  • Trade Count: (+2)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 105
  • Last login:February 28, 2019, 01:19:24 pm
Re: AutoHotkey help please
« Reply #25 on: October 10, 2010, 11:51:28 pm »
Severdhed,

Having experience with two of my own, you should invest in a shock collar for your children  :laugh2:

I'm a software engineer by trade, so it's just a matter of learning the syntax. There is documentation on the Autohotkey website, but it's far from intuitive or complete in my opinion -- hence some of our own questions.

I also don't understand the behavior in MAME vs. Windows, as in why something that works perfectly in logic and via a Notepad test would behave differently in MAME. For example, the space I inadvertantly left between {Blind} and {'keypresscode'} shouldn't have made a load of beans by removing it. As for myself, I've spent the last week trying to get HyperSpin to my liking as my front end -- tackling the software side before building my control panel. HyperSpin uses an optional AutoHotKey script as part of the front end, but I'm having trouble folding my code here into their existing behemoth of a script.

BTW, something you might find useful. Assuming you'd like to limit the 5 key press code to just MAME, you can preserve the key's normal functionality in other software by placing this near the top of the script (outside of the /* and */ comment tags):

#IfWinActive ahk_class MAME

Also note that this script is not cleaned up, in that it will remain loaded until you close Windows, which if using the above directive is not really a big deal.

A clean version of the code would monitor for the termination of MAME and then call the ExitApp function in AHK to terminate the script.

Evan
« Last Edit: October 10, 2010, 11:56:41 pm by wxforecaster »

severdhed

  • Trade Count: (0)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 2971
  • Last login:October 07, 2022, 04:45:32 pm
  • RIP Dinosaur Hippo
Re: AutoHotkey help please
« Reply #26 on: October 11, 2010, 12:04:29 am »
ahh, cool.  would that completely terminate the script, or just make it stop functioning until mame was opened again?  i don't necessarily need to kill the script either way, i very rarely ever do anything on this PC other than mame. (except for admin type stuff, and it is easy enough to manually stop the script by right-clicking the tray icon).  but if it kept the script running but just didn't do anything because mame wasn't running, then i wouldnt have to remember to stop it.

i wanted shock collars, my wife wouldnt go for it :)

as for my other cabinet, should i just remove the else line to make it function only as a delayed exit?

else
{
   send {Blind}{9 downtemp} ;Send MAME pause/unpause
   send {Blind}{9 up}
Current Projects:      Zak-Man | TMNT Pedestal | SNES Pi | N64 Odroid
Former Projects:     4 Player Showcase | Donkey Kong | iCade

wxforecaster

  • Trade Count: (+2)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 105
  • Last login:February 28, 2019, 01:19:24 pm
Re: AutoHotkey help please
« Reply #27 on: October 11, 2010, 07:34:21 am »
EDIT: THIS IS A LONG THREAD. SEE FINAL WORKING CODE NEAR END OF THIS PAGE

I added a lot more flexibility to the code. Basically now you'll hold down the button for X seconds to exit. Let go before that, it pauses. I think this performs a lot better and the user can simply setup their keys at the top. The script also exits when MAME exits via the held keypress.

/*
AUTHOR: WXFORECASTER 10/11/10

DESCRIPTION: DESIGN OVERRIDE SUCH THAT A SINGLE KEY PRESS AND KEY HOLD
             GENERATE DIFFERENT FUNCTIONALITY
*/


/*
BEGIN HOLDESCAPE CODE
SET USER DEFINED VARIABLES BELOW
*/
timeToExit := 2000 ;time in milliseconds (e.g. 2000ms = 2 seconds) key must be pressed before sending exit
emulateKey := 5    ;the key we are pressing/emulating
pauseKey := 9      ;your MAME pause key
exitKey := 5         ;your MAME exit key
exitScript := 0      ;0= leave script running, 1= close script when the MAME exit key is fired,



;Make it so the following code only works if MAME is the active window
#IfWinActive ahk_class MAME

;Establish Hotkey Mapping based on the emulate key above and trigger a chunk of code below (lblPauseExit)
Hotkey, $%emulateKey%, lblPauseExit
return

lblPauseExit:
;Set/restart a timer (checkTimer) in the background, which every 200ms keeps a tally of the total timer count
SetTimer, checkTimer, 200
;Initialize our timer
timeElapsed := 0

;Initiate infinite loop
   loop
   {
;Check to see whether key is being pressed or released
      GetKeyState, keyState , %emulateKey%, P


      if (timeElapsed >= timeToExit) ;We've exceeded our time holding down the button, EXIT MAME
      {
              send {Blind} {%exitKey% downtemp} ;Send MAME Exit
              send {Blind} {%exitKey% up}
                                  if (exitScript = 1)
                                      ExitApp
                                  else
                                      break
      }
      else if keyState = U ;If key is released early, send pause
      {
         send {Blind} {%pauseKey% downtemp} ;Send MAME pause/unpause
         send {Blind} {%pauseKey% up}
         break
      }
            
      sleep 100
   }
      
   timeElapsed := 0 ;Reset timer to zero
   SetTimer, checkTimer, Off ;Turn off timer until next key press
return


checkTimer:
   timeElapsed := timeElapsed + 200
return
 
« Last Edit: October 16, 2010, 11:10:56 am by wxforecaster »

severdhed

  • Trade Count: (0)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 2971
  • Last login:October 07, 2022, 04:45:32 pm
  • RIP Dinosaur Hippo
Re: AutoHotkey help please
« Reply #28 on: October 11, 2010, 12:35:09 pm »
awesome..i'll try this out when i get home this evening.  it's cool you made it use definable variables, which could make this a handy tool for many people here.
Current Projects:      Zak-Man | TMNT Pedestal | SNES Pi | N64 Odroid
Former Projects:     4 Player Showcase | Donkey Kong | iCade

severdhed

  • Trade Count: (0)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 2971
  • Last login:October 07, 2022, 04:45:32 pm
  • RIP Dinosaur Hippo
Re: AutoHotkey help please
« Reply #29 on: October 11, 2010, 09:52:12 pm »
ok, i tried this and it works great....the first time.  however it apparently completely exits the script once mame exits.  this cannot work.  for instance, i launch the script, then launch MALA..i play a game, the script works beautifully, then i exit the game and choose a different game, the script is no longer running, so it stops working.  unless there is another way to do it, the section that ties it together with teh mame window will need to be removed.

also, if did not want this to pause, just exit, what do i do?  i tried just deleting the value for pauseKey, but that just caused it to exit immediately.  i guess i could define it as a key that is not used....unless there is a better way?

Current Projects:      Zak-Man | TMNT Pedestal | SNES Pi | N64 Odroid
Former Projects:     4 Player Showcase | Donkey Kong | iCade

severdhed

  • Trade Count: (0)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 2971
  • Last login:October 07, 2022, 04:45:32 pm
  • RIP Dinosaur Hippo
Re: AutoHotkey help please
« Reply #30 on: October 11, 2010, 10:35:35 pm »
ok, i tried playing around with the script,but i'm clearly too stupid to make this work.  i used a ; before a few lines, which i believe makes the script think they are comments and therefor doesn't process them.

i disabled the line:  #IfWinActive ahk_class MAME

which made it work in notepad also. it sent the 9 key when i pressed it, but when i held it, it sent the 5 key, then exited the script and then it would only send 5 keys.

so i disabled the line: ExitApp

this prevented the script from exiting after the long press, but it still doesnt work in mame.  the first game works fine, but once i exit it, and try to launch another game, it starts up runs for a few seconds, and then exits automatically back to the front end.

i'll keep playing around, but i dont think i know what else to try.
Current Projects:      Zak-Man | TMNT Pedestal | SNES Pi | N64 Odroid
Former Projects:     4 Player Showcase | Donkey Kong | iCade

wxforecaster

  • Trade Count: (+2)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 105
  • Last login:February 28, 2019, 01:19:24 pm
Re: AutoHotkey help please
« Reply #31 on: October 11, 2010, 11:01:02 pm »
How are you spawning the .ahk script.

As it is written, when you quit MAME, the script exists. Thus the 5 key correctly returns to normal operation as a 5 key.

In proper usage, you should be spawning the script when you launch MAME, and terminate it when you close down the game and return to Windows (or your front end).
So, MaLa should be launching the .ahk script (or compiled .exe) when it launches MAME. If the front end cannot launch additional executables, than that in my opinion is a severe flaw in the frontend.

You are correct that a line beginning with ; is a comment in AHK.

Evan
« Last Edit: October 11, 2010, 11:02:42 pm by wxforecaster »

wxforecaster

  • Trade Count: (+2)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 105
  • Last login:February 28, 2019, 01:19:24 pm
Re: AutoHotkey help please
« Reply #32 on: October 11, 2010, 11:03:43 pm »
Accidental Double Post
« Last Edit: October 12, 2010, 12:19:40 am by wxforecaster »

severdhed

  • Trade Count: (0)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 2971
  • Last login:October 07, 2022, 04:45:32 pm
  • RIP Dinosaur Hippo
Re: AutoHotkey help please
« Reply #33 on: October 11, 2010, 11:11:19 pm »
oh...i never thought of trying to launch it that way. i had it set up so that the script ran as a system service when windows boots.  i'm not sure if mala can launch another application when it launches mame..i'll have to look at that.  (it is storming badly here right now, so my cabinets are unplugged at the moment)

i'll see what happens and let you know
Current Projects:      Zak-Man | TMNT Pedestal | SNES Pi | N64 Odroid
Former Projects:     4 Player Showcase | Donkey Kong | iCade

severdhed

  • Trade Count: (0)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 2971
  • Last login:October 07, 2022, 04:45:32 pm
  • RIP Dinosaur Hippo
Re: AutoHotkey help please
« Reply #34 on: October 11, 2010, 11:34:20 pm »
mala doesnt seem to have that functionality, or if it does, i can't find it.  i'm not sure why this would be a feature anyway.  i guess i'll just have to use the older version of the code you posted, since I can run it as a service and just let it run all the time.  i really like MaLa, and i don't want to switch front ends over something like this. 

i like the idea of they way the variables are defined in the new script, which makes it more flexible, but if it can't just be launched and stay running, it isn't going to work for me, or anyone using MaLa  (and possibly other front ends)

i do appreciate all your help.  If you could adjust the new script to function more like the old one, only with definable variables that would be awesome.  if not, thats cool too.

Current Projects:      Zak-Man | TMNT Pedestal | SNES Pi | N64 Odroid
Former Projects:     4 Player Showcase | Donkey Kong | iCade

wxforecaster

  • Trade Count: (+2)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 105
  • Last login:February 28, 2019, 01:19:24 pm
Re: AutoHotkey help please
« Reply #35 on: October 12, 2010, 12:21:27 am »
FINAL CODE VERSION

Here, I actually made this much smaller and simpler, with no timers needed. I didn't realize that if you built a timeout into the hotkey, it will throw an "error" if the hold time is exceeded. The other benefit to this is the timeout is listed in seconds, so one need not due a millisecond calculation.

Here's the final script, including the ability to exit or not exit the script when you dump out of MAME.

/*
AUTHOR: WXFORECASTER 10/12/10

DESCRIPTION: DESIGN OVERRIDE SUCH THAT A SINGLE KEY PRESS AND KEY HOLD
             GENERATE DIFFERENT FUNCTIONALITY
*/


/*
BEGIN HOLDESCAPE CODE
SET USER DEFINED VARIABLES BELOW
*/
timeToExit := 1.6    ;time in seconds key must be pressed before sending exit (this can be a decimal e.g. 1.5)
emulateKey := 5    ;the key we are pressing/emulating. If using letter, be sure to put in quotes (e.g. "n")
pauseKey := 9      ;your MAME pause key.  If using letter, be sure to put in quotes (e.g. "n")
exitKey := 5         ;your MAME exit key.  If using letter, be sure to put in quotes (e.g. "n")
exitScript := 0      ;0= leave script running, 1= close script when the MAME exit key is fired,

;Make it so the following code only works if MAME is the active window
#IfWinActive ahk_class MAME

;Establish Hotkey Mapping based on the emulate key above and trigger a chunk of code below (lblPauseExit)
Hotkey, $%emulateKey%, lblPauseExit
return

lblPauseExit:

KeyWait %emulateKey%, T%timeToExit%

      if ErrorLevel = 1 ;timeout was reached...exit MAME
      {
              send {Blind} {%exitKey% downtemp} ;Send MAME Exit
              send {Blind} {%exitKey% up}
                   if (exitScript = 1)
                      ExitApp
      }
      else
      {
         send {Blind} {%pauseKey% downtemp} ;Send MAME pause/unpause
         send {Blind} {%pauseKey% up}
      }    
return
« Last Edit: October 16, 2010, 11:10:10 am by wxforecaster »

nitz

  • Trade Count: (+2)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 507
  • Last login:November 24, 2015, 07:57:29 pm
Re: AutoHotkey help please
« Reply #36 on: October 12, 2010, 09:04:19 pm »
Thanks for refining this wxforecaster :applaud: I plan to have a similar setup on my cab, so this is gonna come in handy.

severdhed

  • Trade Count: (0)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 2971
  • Last login:October 07, 2022, 04:45:32 pm
  • RIP Dinosaur Hippo
Re: AutoHotkey help please
« Reply #37 on: October 12, 2010, 10:17:44 pm »
ok..it is like 99% perfect.  it is working fine on my showcase cabinet where I am using 5 and 9.  however it will not work on my other cabinet, where n is the exit key.  i tried and tried and couldnt' get it to work.  so i disabled the line that tells it to only work if mame is the active window, so i could see what happens in notepad, here is what i found.

in the section where you define the variables:

BEGIN HOLDESCAPE CODE
SET USER DEFINED VARIABLES BELOW
*/
timeToExit := 1.6    ;time in seconds key must be pressed before sending exit (this can be a decimal e.g. 1.5)
emulateKey := 5    ;the key we are pressing/emulating
pauseKey := 9      ;your MAME pause key
exitKey := 5         ;your MAME exit key
exitScript := 0      ;0= leave script running, 1= close script when the MAME exit key is fired,


if i set it that way and fire up notepad, i press the 5 key, a 9 appears, if i hold the 5 key for a few seconds, a 5 appears...so far so good.  but when i try to define the emulateKey variable as n instead of 5 it stops working.  it just sends an n immediately.  i tried several other letters and the same thing happens.  if i set the emulatekey to a number, like 2 for example, it works fine. but not with letters.  any ideas?  i am using a stupid x-arcade encoder in my other cabinet it is a pain in the butt to work with, so i'd like to keep the button assignments intact.
Current Projects:      Zak-Man | TMNT Pedestal | SNES Pi | N64 Odroid
Former Projects:     4 Player Showcase | Donkey Kong | iCade

wxforecaster

  • Trade Count: (+2)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 105
  • Last login:February 28, 2019, 01:19:24 pm
Re: AutoHotkey help please
« Reply #38 on: October 13, 2010, 01:26:43 am »
If you use a character instead of a number, I'm fairly certain is has to be quoted?

e.g.

thisVariable := "n"

severdhed

  • Trade Count: (0)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 2971
  • Last login:October 07, 2022, 04:45:32 pm
  • RIP Dinosaur Hippo
Re: AutoHotkey help please
« Reply #39 on: October 13, 2010, 09:58:52 pm »
That did it!!!  i did not define a pause variable for the second cabinet, since it has a separate pause button, and now both cabinets work perfectly..thank you so much for all your work.  this is an awesome script, and i'm sure it will come in handy for others as well.  THis is really great...thank you so much.

this script should be kept somewhere where people can find it easily, there should be a repository for things like this on the boards.

thanks again.
Current Projects:      Zak-Man | TMNT Pedestal | SNES Pi | N64 Odroid
Former Projects:     4 Player Showcase | Donkey Kong | iCade