Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Windows .exe loses focus to AM if keys are pressed during launch #220

Closed
victor-thompson opened this issue May 6, 2016 · 6 comments
Closed

Comments

@victor-thompson
Copy link

Hi, first time Attract Mode user and poster here! I'm building a cabinet that will run a handful of locally-made games under Windows 8.

We're having a problem where AM keeps/steals focus if the player starts a game and then presses keys during the brief fade-down that AM does.

There are two flavors of this:

  1. Attract Mode (just a black screen at this point) stays in the foreground and has focus;
  2. The game is in the foreground, but Attract Mode has focus and is eating keyboard input.

I just updated to 2.0.0 from 1.6.4, no change.

I've tried:

  • launching the .exe with "command /c [romfilename]"
  • launching via a .bat that starts the .exe
  • variations of both of those with various "Minimum Run Time" values

I'm kind of at a loss for other ideas! Any help is appreciated.

Thanks!

@ianfitzpatrick
Copy link

ianfitzpatrick commented May 14, 2016

FWIW I believe we ran into this too at the arcade we setup. Luckily we were on hand to correct if it happened. But for folks setting up permanent installations without supervision around, this is pretty deal breaker problem to have :(

@zachstronaut
Copy link

Would love to get a response on this issue! Is there a way to make sure that games are launched in the foreground with keyboard focus on Windows when using Attract Mode?

@mickelson
Copy link
Owner

Ok, so I have tried to reproduce this using the most recent attract-mode, mame, and the orbit theme (which has a fade to game effect) on windows 10. I'm just using the keyboard keys, and so far I am unable to reproduce. I'm launching the program and then mashing random keys while the "fade out" occurs.

Are you able to reproduce this reliably or is it a relatively rare occurrence? Also, please post the emulator configuration file that you are running the windows programs with. thanks.

@zachstronaut
Copy link

zachstronaut commented May 23, 2016

Hi Andrew thanks for your help!

I'm using the 2.0.0 download from the website and the orbit theme on Windows 8. I'm however launching Windows games using the windows_games emulator and NOT using MAME.

I can recreate the bug 100% of the time by pressing enter to launch a game and then immediately spamming the Up key on my keyboard. The game will either launch in the background or it will be in the foreground but without keyboard focus. I'm seeing this across several games built with different engines (Unity and others).

My windows_games emulator file is attached.

windows_games.zip

@victor-thompson
Copy link
Author

I've been working on a fix on my own, and it looks like one method would be to just find and focus the root window of the launched process (or, if that process is cmd /c and doesn't have a window, its child process).

It's pretty ugly code still, but it seems to fix the problem.

For reference and verification, my emulator config attached:
windows_games.cfg.txt

@victor-thompson
Copy link
Author

OK, I am now confident that my change is dealing with the problem. For myself, that's good and I'll continue to use an altered version of the launcher.

I don't think my code is necessarily what you'd want to use if you choose to address this in the main repo (let me know if you want it anyway). The gist is that Attract Mode has focus, and therefore can grant focus to whichever other process it would like via SetForegroundWindow, SetWindowPos, or what have you.

The HWND you want to pass focus to may belong to a child of the child process launched by AM, so you have to grovel around in various enumeration functions (I used CreateToolhelp32Snapshot and EnumWindows as my base, with GetAncestor to deal with weird apps that don't create their top-level window first).

Finally, since the window may not be created right away, run_program needs to try this whole process periodically in it's wait loop (every one or half-second works for me).

Let me know if you want more details!

CreateToolHelp32Snapshot:
https://msdn.microsoft.com/en-us/library/windows/desktop/ms682489%28v=vs.85%29.aspx
EnumWindows:
https://msdn.microsoft.com/en-us/library/windows/desktop/ms633497%28v=vs.85%29.aspx
GetAncestor:
https://msdn.microsoft.com/en-us/library/windows/desktop/ms633502%28v=vs.85%29.aspx

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants