# [.net] C# Windows API WM_KEYDOWN SPY++

This topic is 3495 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hello, First, my goal is to get CTRL + T to work. Here is what I get from Spy++ when I do it manually.
WM_KEYDOWN nVirtKey:VK_CONTROL
WM_KEYDOWN nVirtKey:'T'
WM_KEYUP nVirtKey: 'T'
WM_KEYUP nVirtKey:VK_CONTROL

Now when I run my C# code I get this
WM_KEYDOWN nVirtKey:VK_CONTROL
WM_KEYDOWN nVirtKey:'T'
WM_KEYUP nVirtKey: 'T'
WM_KEYUP nVirtKey:VK_CONTROL
WM_CHAR chCharCode:'116' (116)

Note the extra line of code, and everything is 100% identical. Now for my C# Code.
PostMessage(m_MainWindowHandle, WM_KEYDOWN, VK_CONTROL, (IntPtr)0x001D0001);
PostMessage(m_MainWindowHandle, WM_KEYDOWN, VK_T,(IntPtr)0x00140001);

I know, I don't have the key up events, but I tried with and without them, it seems as though if I do manually T + CTRL I get that charCode. Should there be something in between that I am missing? Thanks

##### Share on other sites
Have a look at SendKeys

##### Share on other sites
Head, I don't mean to offend you, but I just gave a extremely detailed purpose/information. And you give me the most basic answer. I know about SendKeys, but it requires focus. No thanks. There are ways to do it the right way, not the easy way.

Anyone else?

##### Share on other sites
You didn't give an "extremely detailed purpose/information" because if you did I would have known the window didn't have focus and that SendKeys was not a valid method. If you don't want someone to recommend something then give the necessary information about what your doing. If the window doesn't have focus, then say that in your post, don't expect people to read your mind then rip into them when they make a relevant suggestion based on what you've posted.

##### Share on other sites
I belive hot keys would work for what your looking for
pritty shure they work without focus but i havent used them in a while
http://msdn.microsoft.com/en-us/library/ms646309.aspx

edit: wait you want to send keys to an unfocused window? woops >_<

##### Share on other sites
@Kenster

that was kind of a douchey way to respond to someone that was just trying to help. he's going out of his way to read your problem and respond to it, regardless of the suggestion he gave you, you should still be grateful that at least you got a response. Maybe you hadn't heard of SendKeys and that was a valid solution that you didn't know about. How was he to know what you already knew? You didn't (and still haven't) specify anything about the application you're trying to send keystrokes to. Just because an application doesn't have focus, it doesn't mean you can't make that happen procedurally. In fact, in C#, it only takes one line of code - SetFocus(m_MainWindowHandle). If you went with that solution, the whole function would only be two lines long:

void SendYourKeys(Keys k) {
SetFocus(m_MainWindowHandle);
SendKeys(k);
}

and that's it. You could even return focus to the previously active window if you so desire. I don't know anything about your set up but in most cases (and not just with programming either) the simplest solution is quite often the best solution. In most cases, if you can get around using window's messages, you should. Leave the windows programming to microsoft, if you can. Try to use what they provide you before trying to reinvent the wheel.

we want to encourage people to respond to posts, not discourage them with fear of being flamed. The gamedev forums have pulled through for me many times and I'd like to keep that positive/helpful atmosphere going if at all possible!

.max

ps. your post was relatively barebones (not "extremely detailed purpose/information" like you claim). The only thing we know is that you're trying to use windows messages and failing. =/

##### Share on other sites
Another thing worth mentioning is that just because you can view the messages a window receives in Spy++ doesn't mean that is the method the window is using to read key input. If the app is using DirectInput or RawInput for example you can't use PostMessage.

You could try something like this but whether or not it works is a different story.

void PressCtrlT(HWND hwnd){	PostMessage(hwnd, WM_KEYDOWN, VK_CONTROL, 0);	PostMessage(hwnd, WM_KEYDOWN, VkKeyScan('t'), 0);	sleep(40);	PostMessage(hwnd, WM_KEYUP, VkKeyScan('t'), 0);	PostMessage(hwnd, WM_KEYUP, VK_CONTROL, 0);}

1. 1
2. 2
3. 3
4. 4
Rutin
17
5. 5

• 11
• 30
• 12
• 12
• 11
• ### Forum Statistics

• Total Topics
631408
• Total Posts
2999922
×