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 --- Bug Reports --- Site News

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

  

Author Topic: How to send keystrokes to MAME?  (Read 5755 times)

0 Members and 1 Guest are viewing this topic.

ahofle

  • Trade Count: (+1)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 4540
  • Last login:Yesterday at 09:57:19 pm
    • Arcade Ambience Project
How to send keystrokes to MAME?
« on: August 14, 2007, 03:39:33 pm »
I am trying to get some keystrokes sent to MAME via an external application.  I have tried SendKeys, SendInput, and even keybd_event.  They all work fine in windows apps like Notepad, but MAME doesn't respond at all.  I know keybd_event works for the special LED keys like numlock (ledutil uses it), but I can't get other keys sent.  Any hints would be appreciated.

youki

  • Trade Count: (0)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 1612
  • Last login:November 19, 2016, 01:07:33 pm
  • Atomic Front End Creator
    • Atomic Front End
Re: How to send keystrokes to MAME?
« Reply #1 on: August 14, 2007, 04:15:56 pm »
You can  use a DirectInput hook.


ahofle

  • Trade Count: (+1)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 4540
  • Last login:Yesterday at 09:57:19 pm
    • Arcade Ambience Project
Re: How to send keystrokes to MAME?
« Reply #2 on: August 14, 2007, 05:05:57 pm »
Thanks, can you elaborate on that a bit?  I have the key hooking part already working, I am just trying to send keystrokes.

headkaze

  • Trade Count: (0)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 2943
  • Last login:Today at 01:13:32 pm
  • 0x2b|~0x2b?
Re: How to send keystrokes to MAME?
« Reply #3 on: August 15, 2007, 01:56:41 am »
You don't need a key hook, you need an API hook. Then you need to write wrappers around all the exported functions of DINPUT.DLL.

The function to inject keys is called GetDeviceState(). And the code looks like this. Have a search on the web for APIHijack or for example source go here. FYI keybd_event() worked for Mame < 0.118.

Code: [Select]
HRESULT _stdcall CKeyboardWrapperA::GetDeviceState(DWORD cbData, LPVOID lpvData) {
int nRetVal;
int nPlayer;
BYTE *Keys;

// Get the real keyboard state.
nRetVal = m_lpDIDevice->GetDeviceState(cbData,lpvData);
Keys = (BYTE *)lpvData;

// Inject keystrokes here by altering the values of the Keys array.
//
// Example:
//
// Keys[DIK_A] = 0x80;
//
// Tells the applicatin that the 'A' key is pressed.

return(nRetVal);
}

ahofle

  • Trade Count: (+1)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 4540
  • Last login:Yesterday at 09:57:19 pm
    • Arcade Ambience Project
Re: How to send keystrokes to MAME?
« Reply #4 on: August 15, 2007, 11:14:07 am »
Thanks headkaze, man that's a lot of code just to simulate a single keypress LOL.  I still don't understand why I can't get keybd_event to work.  :banghead:  Oh well, time to read up on direct input.  I'm also going to look into RawInput at Aaron's recommendation.

headkaze

  • Trade Count: (0)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 2943
  • Last login:Today at 01:13:32 pm
  • 0x2b|~0x2b?
Re: How to send keystrokes to MAME?
« Reply #5 on: August 15, 2007, 02:24:59 pm »
Thanks headkaze, man that's a lot of code just to simulate a single keypress LOL.  I still don't understand why I can't get keybd_event to work.  :banghead:  Oh well, time to read up on direct input.  I'm also going to look into RawInput at Aaron's recommendation.

If you find a way to inject keys to the raw input system of 118+ please let me know. I've had a brief look but so far have found nothing.

joeman

  • Trade Count: (0)
  • Jr. Member
  • **
  • Offline Offline
  • Posts: 3
  • Last login:September 05, 2007, 03:23:14 am
  • I want to build my own arcade controls!
Re: How to send keystrokes to MAME?
« Reply #6 on: September 05, 2007, 03:18:47 am »
Have you tried MortScript? It's freeware.

headkaze

  • Trade Count: (0)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 2943
  • Last login:Today at 01:13:32 pm
  • 0x2b|~0x2b?
Re: How to send keystrokes to MAME?
« Reply #7 on: September 05, 2007, 04:26:47 am »
Check out the release notes for 118

Quote
Changed the Windows implementation of input handling to fully
    support the raw input interfaces for keyboard and mouse.
    DirectInput is still used for all joystick inputs, as well as for
    keyboard and mouse inputs on pre-Windows XP systems. This allows
    for multiple keyboards and mice to be supported. Also changed
    keyboard and mouse behavior to use non-exclusive mode in
    DirectInput, and to keep the devices alive during pause for more
    consistent input handling.

This means Direct Input hooking no longer works anyway. But about a week ago I figured out how to hook the Raw Input API's and inject keys. Sorry, but I won't be sharing that info on here (too much potential for abuse).

ahofle: I understand you wanted to do this so you could catch the coin drop keys then play a coin drop sample then inject the keys back in. I could write a program to do this now, but more than likely I will add it as a feature into CPWizard. If you can find some nice coin drop sounds send them to me :)
« Last Edit: September 05, 2007, 04:28:53 am by headkaze »

ahofle

  • Trade Count: (+1)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 4540
  • Last login:Yesterday at 09:57:19 pm
    • Arcade Ambience Project
Re: How to send keystrokes to MAME?
« Reply #8 on: September 05, 2007, 10:15:51 am »
Cool, nice work!   I only briefly looked at the Raw Input stuff and decided it wasn't worth the effort.  Glad you figured it out.  I'll look around for some good samples.  I might also just make my own at the local arcade.  I will let you know.

tapleysj

  • Trade Count: (0)
  • Jr. Member
  • **
  • Offline Offline
  • Posts: 1
  • Last login:September 05, 2019, 07:23:23 am
  • I want to build my own arcade controls!
Re: How to send keystrokes to MAME?
« Reply #9 on: August 31, 2019, 08:06:04 am »
Replying to a super old post here, but I figured this out running MAME 0.212 on a Win 10 machine

Ensure you start MAME up with this option
-keyboardprovider dinput

Then you can use the Windows SendInput API using direct api key codes (instead of VK_*)

See the code on my github account if anyone is interested

https://github.com/stevetapley/mame-ai/tree/master/SendInputDemo

Rahzel

  • Trade Count: (0)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 166
  • Last login:June 02, 2020, 07:16:18 am
  • MAME4LiFE
Re: How to send keystrokes to MAME?
« Reply #10 on: November 06, 2019, 09:31:36 pm »

Ensure you start MAME up with this option
-keyboardprovider dinput


I just came across this through a google search and it worked and will save me so much running back and forth to my cab. Thank you!