Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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:

http://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.

share|improve this question
    
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. –  Patrick Hughes Jun 23 '11 at 0:48
    
It's worth pointing out that you can create your own instances of the KeyboardState struct. –  Andrew Russell Jun 23 '11 at 1:14
    
Are you planning to interface with your own game or other peoples' games? –  Jonathan Hobbs Jun 23 '11 at 1:23
    
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. –  Corey Ogburn Jun 23 '11 at 2:26

2 Answers 2

up vote 1 down vote accepted

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.

share|improve this answer

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.