Replace key input in external program

Hi you all, I hope you can help me.

We've got an application that outputs a period (.) while pressing the period on the numeric keyboard. But I want to replace that period with a comma (,)
So I want to listen on when someone presses the period on the numeric part of the keyboard...and I want to change this to a comma

That's one part of my question...the second part of my question is...I only want it to occur when a certain application has the screen focus. When the application looses the focus, the period key should respond with a period (.) again.

Could you please help me? I've been trying for several days with things like findwindow etc. but I just can't get it to work. Using C++

Thx!
Chris
You need to install a global keyboard hook. Google around SetWindowsHookEx().

Too bad the app isn't internationalized. What is it?
Yeah, it's a real shame....it's a financial application for at work. They replaced the old suite with a suite from CODA. And here we use the comma in stead of a period. They offer a stupid solution, you have to change the keyboard layout whenever you want to use the fin. app. and if you switch to MS Word i.e. then you have to change it back...and they can't solve it. CODA is a webbased application btw. And I want to prove them otherwise...I've already succesfully made it work in WinBatch...but I want a more stable platform.

I'll look into the SetWindowsHookEX()...thx for the tip!
Last edited on
Wow, CODA looks nice. Do you think it might be possible to write a small CODA plug-in that modifies the financial app?

With the Windows hook, you can choose whether or not to modify the user's input just by checking against the currently-focused window. The problem with hooks is that anti-virus software will give your users a hard time about it.
You don't need global hook.
A LL hook works perfectly.
Win32 C code has been posted n times for decades...
"You don't need a global hook", eh?

Prove it.
Now this probably is NOT the best code, BUT! It does work (for me) & I hope that this helps, the code is heavily commented - if you need any more explanation for it, let me know:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include<windows.h> // define windows
#ifndef VK_OEM_PERIOD // if the full-stop (period) key is not defined
#define VK_OEM_PERIOD 0xBE // define it
#endif // end definition
#ifndef VK_OEM_COMMA // if the comma key is not defined
#define VK_OEM_COMMA 0xBC // define it
#endif // end definition
#ifndef VK_BACK // if the backspace key is not defined
#define VK_BACK 0x08 // define it
#endif // end definition
HWND hwnd_ext; // define the external window's handle
int main() // main function
{
hwnd_ext=FindWindow(NULL,"app"); // set the handle to the window with the title "app", change this as necessary
if(hwnd_ext==NULL) // if the window does not exist
{
if(MessageBox(NULL,"The Application Window Is Not Open\nClick \"OK\" once it is open\nClick cancel to exit","Error",1)==2) // give an "OK" "Cancel" box
{
return 0; // if cancel, end the program
}
main(); // if it is okay, check again for the window (this might cause a stack overflow if you press "OK" enough... LOTS of times ;)
}
while(true) // we've reached our main program loop, while true (infinite loop)
{
if(GetAsyncKeyState(VK_OEM_PERIOD)) // get the key state of the full-stop (period) key, if it is true
{
if(GetForegroundWindow()==hwnd_ext) // & the foreground (active) window is our message box
{
keybd_event(VK_OEM_PERIOD,0x45,KEYEVENTF_KEYUP,0); // send the input for key up on the full-stop (period)
keybd_event(VK_BACK,0x45,0,0); // send backspace (delete the fullstop)
keybd_event(VK_OEM_COMMA,0x45,0,0); // send comma
}
}
Sleep(1); // sleep for 1 millisecond, this should stop CPU usage going huge
}
return 0; // end of program
}
// references
// keybd_event - http://msdn.microsoft.com/en-us/library/ms646304(VS.85).aspx
// GetAsyncKeyState - http://msdn.microsoft.com/en-us/library/ms646293(VS.85).aspx
// FindWindow - http://msdn.microsoft.com/en-us/library/ms633499.aspx 
Topic archived. No new replies allowed.