• Announcements

Archived

This topic is now archived and is closed to further replies.

Tricking the Windows Keyboard

Recommended Posts

how can i make my program tell windows that a key on the keyboard has been pressed? i know this is possible because many joysticks (like the sidewinder gamepad and the gravis exterminator) have an option where a button on the keyboard corresponds to a set of keystrokes. i would really like to know how to do this... thanks Mad programmer

Share this post

Share on other sites
WarMage    122
The Sidewinder in specific uses the DirectInput API. This is certainly one method, another windoze method is to post window messages to the system and let it do whatever that crazy windows-message be doin''.

-------------------
-WarMage
...always a playa, unless I''m an argentinian prarie.

Share this post

Share on other sites
how would i use to dinput api to do that?
also, i tried w/ the windows messages, but i don''t think they are going anywhere... what hWnd would i use when calling PostMessage() to get it to go to whatever window is in the foreground? (not necissarily my program)

heres the code i have right now.....

for(int i=''A'';i<=''Z'';i++)
PostMessage(NULL, WM_KEYDOWN, i, 0);

thanks for your help....

the madprogrammer

Share this post

Share on other sites
Funkymunky    1413
hwnd = GetForegroundWindow();

Share this post

Share on other sites
it still doesn''t work....

for(int i=''A'';i<=''Z'';i++)
{
PostMessage(GetForegroundWindow(), WM_KEYDOWN, i, 0);
Sleep(100);
}
MessageBox(NULL, "Done", "Done", 0);

Share this post

Share on other sites
Dean Harding    546
You can''t use PostMessage(), since it will post the message to your own thread''s message queue. You need to post the message to the thread of the foreground window, as in:

  HWND hwndFocus = GetForegroundWindow(); DWORD dwThread = GetWindowThreadId( hwndFocus ); PostThreadMessage( dwThread, WM_KEYDOWN, ''A'', 0 );

War Worlds - A 3D Real-Time Strategy game in development.

Share this post

Share on other sites
that looks like it would work perfectly, but where is GetWindowThreadId()? My compiler can''t find it.

thanks.

the MadProgrammer.

Share this post

Share on other sites
laotzu    122
Looking on msdn, it shows that GetWindowThreadId() is only available in the win ce os. So I tried using GetWindowThreadProcessId(), leaving the rest of the code the same just switching those to functions.. this compiles, But no output appears in the other window... any help would be appreciated as I think this sounds neat.
Thanks,
lao

Share this post

Share on other sites
laotzu    122
Oh, I should say that I was trying to send the message to a window that did not belong to my process.. I don''t know if that is possible? Or if you can only send them to your child windows.

Share this post

Share on other sites
i know it is possible to make other processes think that the keyboad is being pressed b/c a lot of gamepads & joysticks can do it. i just wana know how......

Share this post

Share on other sites
Guest Anonymous Poster
I don''t know how much this will help, but you may want to
try SendMessage() instead of PostMessage().
PostMessage() sends a message to the queue where it simply
waits to be processed.
SendMessage() translates the message and calls the message
handler immediately.

Also, if you want a specific input to correspond to a number
of keystrokes, you might be able to use the message handler
directly.
The handler is usually made into a big switch statement so
you should be able to clump the input and the specific
keyboard messages.

Also again, Windows tells your program that a key has been hit.
Not the other way around unless your program doesn''t have a
specific way to deal with that message so it uses a default
handler.

Finally, the foreground window, whatever it is will have to know
what to do with the message when it gets it, I don''t know if
one program can send messges to another though.

Share this post

Share on other sites
i want to tell another process that a key has been hit, not my own process. that would be easy.

Share this post

Share on other sites
Guest Anonymous Poster
A lowlevel device driver hooked directly with the kernel. Sound like fun to code though

Share this post

Share on other sites
is there an easier way?