Retriving a key pressed in a different Application

<Gal Beniamini@discussions.microsoft.com> wrote in messagenews:d1c96286-5525-41a4-9d28-579d9d7d3011@discussions.microsoft.com > Hello, I have a question; how is it possible to get the event of a> key pressed in a different application while my application is> running in the background and to then identify which key it was? You need to install a system-wide hook - see SetWindowsHookEx-- With best wishes, Igor Tandetnik
[436 byte] By [MVPUser] at [2008-3-7]
# 1
Hello, I have a question; how is it possible to get the event of a key pressed in a different application while my application is running in the background and to then identify which key it was? (Please include an example, if exists).
With Thanks,
Gal Beniamini.
GalBeniamini at 2007-9-9 > top of Msdn Tech,Visual C++,Visual C++ Language...
# 2
Hello, and thanks for your help. Although you have given me a great answer I still have some questions;
Firstly, is this supported in .net Framework 2?
Secondly, the example for the SetWindowsHookEx was really complicated, could you please help me find an example that only includes the Keyboard Hook or write one here? (If it helps, I only want the keys pressed in an app called "xen.exe").
With Thanks,
Gal Beniamini.
GalBeniamini at 2007-9-9 > top of Msdn Tech,Visual C++,Visual C++ Language...
# 3

Hi Gal,

you could maybe create a thread that runs in the background and listens for key presses, I know I did something similar but for mouse events, it might be that the principle is similar, let me know if you'd be interested in what I have.

Yacine Benahmed

YacineBenahmed at 2007-9-9 > top of Msdn Tech,Visual C++,Visual C++ Language...
# 4
Hello Yacine Benahmed,
Thanks very much for your offer, I would appreciate it greatly if you were to guide me as to how to create what you've written about (just with keyboard events).
With Thanks,
Gal Beniamini.

GalBeniamini at 2007-9-9 > top of Msdn Tech,Visual C++,Visual C++ Language...
# 5

Hi Gal,

I veryfied my code, however I dont know how you could modify it to listen to keypress events.



private: void Create_MouseListener()
{
th_MouseListener = new Thread(new ThreadStart(this, &Form1::Listen_To_The_Mouse));
th_MouseListener->set_Priority(ThreadPriority::Lowest);
th_MouseListener->Start();
}

private void Listen_To_The_Mouse()
{
String *position;
String *str_xy[] = {S"",S""}; //needed for the string containing the coordinates
String *sep = " , "; //separates x y
while(1) //infinite loop
{
th_MouseListener->Sleep(1);
//Process sleeps 10ms or else takes to much cpu time
str_xy[0] = System::Convert::ToString(Control::MousePosition.get_X());
str_xy[1] = System::Convert::ToString(Control::MousePosition.get_Y());
position = String::Join(sep, str_xy);
txt_Valeur->Text = position;
txt_Valeur->AppendText(System::Convert::ToString0(Control::get_MouseButtons()));
System::GC::Collect(); //garbage collection
}
}



Yacine Benahmed

YacineBenahmed at 2007-9-9 > top of Msdn Tech,Visual C++,Visual C++ Language...