SEVI Posted January 24, 2004 Posted January 24, 2004 Hi Wondering if anyone knows how to accept keyboard input in C# without having to have a given form in focus. I've 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. I'm trying to detect Left Right and spacebar keystrokes and then raising button click events when the project's process is running no matter what has the focus. Thanks SEVI Quote
bpayne111 Posted January 25, 2004 Posted January 25, 2004 sounds like a job for the api... i'd give you a good api resource or something but i don't have one at the moment. get to searching :) brandon Quote i'm not lazy i'm just resting before i get tired.
NK2000 Posted January 25, 2004 Posted January 25, 2004 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 Quote
sde Posted January 25, 2004 Posted January 25, 2004 i recently was attempting to do somethign like this. here are the only resoruces i've found regarding this: http://www.codeproject.com/csharp/NetWin32Hooks.asp http://msdn.microsoft.com/library/en-us/winui/winui/windowsuserinterface/windowing/hooks.asp hope this helps. Quote codenewbie
NK2000 Posted January 25, 2004 Posted January 25, 2004 i can just say thanks, because i am sure i will need that information soon :) Thanks :) Quote
SEVI Posted January 26, 2004 Author Posted January 26, 2004 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 won't 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 Quote
NK2000 Posted January 26, 2004 Posted January 26, 2004 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 Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.