Link to home
Start Free TrialLog in
Avatar of SonicX_BlueBlur
SonicX_BlueBlur

asked on

Keyboard Input

How do I get keyboard input from anywhere inside a form?

I know it's kind of a dumb question, but I'm a C++ programmer, and not much of a C# one :P
Avatar of BurntSky
BurntSky

Throw this in the form's constructor or some other initialization method (or you can do it via the events tab in the properties window in the designer):

this.KeyDown += new KeyEventHandler(ManualPropertiesDialog_KeyDown);

And then add this method:

private void ManualPropertiesDialog_KeyDown(object sender, KeyEventArgs e)
{
      MessageBox.Show(e.KeyCode);
}
... and unfortunatelly it will not work at all since how you're supposed to get your focus on your form if you have any controls on it and if you have any controls, they will try to handle your keyboard input.

there can be several ways to go, but you didn't give all details, like you want to capture your keyboard input inside your controls by controls or by form etc.

the following will capture all keyboard input in the form regardless of the current focus:

// you have to implement PreFilterMessage:
public bool PreFilterMessage( ref Message m )
{
      Keys keyCode = ( Keys )( int )m.WParam & Keys.KeyCode;
      if( m.Msg == WM_KEYDOWN )
      {
            this.statusBar1.Text = keyCode.ToString();
      }
      return false;
}

regards
Avatar of SonicX_BlueBlur

ASKER

Yurich: That's the kind of stuff I want; non-specific keyboard input throughout the form, but only if the form is active (we don't want a keylogger, just formwide keyboard control). Now, I can't try it right now, but I'll let you know and give you points as soon as I test it and it works.
ASKER CERTIFIED SOLUTION
Avatar of jatinderalagh
jatinderalagh
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
it will not capture messages that your controls are going to handle...
We were talking about keys And this method get all the keys.

what you are doing is you are filtering the messages for keys input .
And i am getting filtered messages for keys.

And what burntSky has provided that also gona work for all the keys on form as control's keys event are fired only after the form's keyEvent.
You just need to set the Form's KeyPreview property to true. For using that event.
Using event or overriding  ProcessCmdKey method depends on the refined control you require for your application
Yurich: OK, so if a control has a KeyPress handler, it won't catch the event using your code? Or the other way around, all KeyPress handlers will be overridden?

Either way, that's fine.
i dont know why you guys are making it complex .NET provides an easy way to accomplish your desired task.

select your form go to properties window
set its "KeyPreview" property to true, now your form will get all Keyboard events no matter foucus is on a texbox or button or on form.

Now add an event handler of form's "KeyPress"
e.keyChar //  char that is pressed

private void Form1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
     // do what ever you want to do
 
   
    //set e.Handled = true if you dont wanna pass it further to control where the
    // keyboard focus was        

    //  
    //  you can use  this.ActiveControl to get the control where focus is
}



mwaseem2k's way will work is fine as well.

the way I showed WILL NOT FILTER OUT anything.

you still can use key_press or key_down for your controls and it'll be still there, e.g.

if you have a bunch of textboxes and you want to trigger something when something is entered in any of the boxes, you can use my way and whatever is typed will be still shown in the the textboxes

regards
The KeyPreview method doesn't work for every key (case in point, cursor keys). I ended up using the ProcessCmdKey override, it works fine.
Looks like you accepted some thing else.
...Oh, dang it. Wasn't paying attention >_<

Well, time to get this fixed. Sorry, jatinderalagh.
There you go, jatinderalagh. 125 points with A-grade for you :)
Yeah, sorry again. I was kind of in a hurry to award those points, had other things to do.
Thanks

;)
JAtinder