5
\$\begingroup\$

I want to be able to control the game via keyboard while the game does not have focus... How can I do this in XNA?

EDIT: I bought a tablet. I want to write a separate app to overly the screen with controls that will send keyboard input to the game. Although, it's not sending the input DIRECT to the game, it's using the method discussed in this SO question:

https://stackoverflow.com/questions/6446085/emulate-held-down-key-on-keyboard

To my understanding, my test app is working the way it should be but the game is not responding to this input. I originally thought that Keyboard.GetState() would get the state regardless that the game is not in focus, but that doesn't appear to be the case.

\$\endgroup\$
4
  • \$\begingroup\$ What is it you're trying to do specifically? I may be able to suggest other architectures that work better than ignoring the user's wish to type in another window. I assume that this is a windows PC platform question and not XBox problem. \$\endgroup\$ Jun 23, 2011 at 0:48
  • \$\begingroup\$ It's worth pointing out that you can create your own instances of the KeyboardState struct. \$\endgroup\$ Jun 23, 2011 at 1:14
  • \$\begingroup\$ Are you planning to interface with your own game or other peoples' games? \$\endgroup\$ Jun 23, 2011 at 1:23
  • \$\begingroup\$ I want it to be like Joy2Key (electracode.com/4/joy2key/JoyToKey%20English%20Version.htm), except instead of a physical gamepad, a virtual one. So it wouldn't directly interact with the game. I suppose more effort should be in that app working with my game instead of vice versa. \$\endgroup\$ Jun 23, 2011 at 2:26

2 Answers 2

1
\$\begingroup\$

Probably the safest way of doing that would be a little IPC (inter-process communication) that your game starts and listens to and your overlay tries to find and sends messages to.

The exact API to create that channel is going to be operating system specific. Wikipedia gets into some elaborate software, but something as simple as a shared memory area (with thread safe locks controlling access) and a shared data structure to pass the information along would work great.

\$\endgroup\$
0
0
\$\begingroup\$

If your game does not have focus, it should not be reading input - that is a giant programming taboo.

However, it is possible. Use P/Invoke to call SetWindowsHookEx with the parameter WH_KEYBOARD (and dwThreadId set to null). This will call your hook event on every keypress, no matter what window has focus at the time.

Again, though, I am not recommending this. It is quite likely this will even trigger some virus scanners.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .