Processing keystrokes regardless of focus

SEVI

Well-known member
Joined
Jun 28, 2003
Messages
48
Location
Australia
Hi

Wondering if anyone knows how to accept keyboard input in C# without having to have a given form in focus.

Ive found the KeyPress, Up and Down events and also the IMessageFilter interface to work however they cease to function when the form on which they are implmented loses focus.

Im trying to detect Left Right and spacebar keystrokes and then raising button click events when the projects process is running no matter what has the focus.

Thanks

SEVI
 
sounds like a job for the api... id give you a good api resource or something but i dont have one at the moment.

get to searching :)
brandon
 
i dont know a way to use it in win32 but there are some good ways to get them using directinput, just a look in the SDK samples will show you how to solve that
 
Thanks all. Some of this stuff looks quite involved given the return it will provide and on second thoughts it may cause dramas as if the keystrokes are intercepted globally then it means if the keystroke was intended for the operating system then it wont get there.

I also looked at forcing the form to always have focus and then to just use the keypress event which is a round about way of doing the same thing. The odd thing is when I try to do this using Form.Activate events upon form_Deactivate the focus goes back to the form but keystrokes are not sent to the form until it is clicked on using the mouse. See the code below:

public class frmConsole : System.Windows.Forms.Form {
private System.ComponentModel.IContainer components;

public frmConsole() {

InitializeComponent();
}

protected override void Dispose( bool disposing ) {
if( disposing ) {
if (components != null) {
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
private void InitializeComponent()
{
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(264, 269);
this.Name = "frmConsole";
this.ShowInTaskbar = false;
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "Console";
this.TopMost = true;
this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.frmConsole_KeyDown);
this.Deactivate += new System.EventHandler(this.frmConsole_Deactivate);

}
#endregion

[STAThread]
static void Main() {
Application.Run(new frmConsole());
}

private void frmConsole_Deactivate(object sender, System.EventArgs e) {
this.Activate();
}

private void frmConsole_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e) {
MessageBox.Show("Keystroke intercepted");
}
}

Is this as good as it gets or is Activate() the wrong method to use?

Thanks
 
it may cause dramas as if the keystrokes are intercepted globally then it means if the keystroke was intended for the operating system

i didnt look this much up but i think it is easily possible to check if the wanted form has focus/is active or not?

i think you should check that, which could be easier for applications where you dont have the source off and cant modify!

you can also activate a form and if you want a mouse click you even can emulate them, spy++ can also display events

you can even get the current size of window and translate that to screen coordinates if you want to activate by emulated mouse click

normal the "normal" activation of windows is easier then by doing it per mouse but it would be also a good training :D

hope that helps
 
Back
Top