• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 590
  • Last Modified:

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
0
SonicX_BlueBlur
Asked:
SonicX_BlueBlur
  • 5
  • 5
  • 3
  • +3
1 Solution
 
BurntSkyCommented:
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);
}
0
 
YurichCommented:
... 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
0
 
SonicX_BlueBlurAuthor Commented:
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.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
jatinderalaghCommented:
If you just want to get the keys when your form is active. just override following method of your form class.


protected override bool ProcessCmdKey(   ref Message msg,   Keys keyData)
{
// Do your processing of keys in here

...
...

//call base method

base.ProcessCmdKey( msg,keyData);

}
0
 
YurichCommented:
it will not capture messages that your controls are going to handle...
0
 
jatinderalaghCommented:
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.
0
 
jatinderalaghCommented:
Using event or overriding  ProcessCmdKey method depends on the refined control you require for your application
0
 
SonicX_BlueBlurAuthor Commented:
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.
0
 
mwaseem2kCommented:
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
}



0
 
vinhnlCommented:
see this sample: http://www.codeproject.com/csharp/GlobalSystemHook.asp

Good luck
VINHNL
0
 
YurichCommented:
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
0
 
SonicX_BlueBlurAuthor Commented:
The KeyPreview method doesn't work for every key (case in point, cursor keys). I ended up using the ProcessCmdKey override, it works fine.
0
 
jatinderalaghCommented:
Looks like you accepted some thing else.
0
 
SonicX_BlueBlurAuthor Commented:
...Oh, dang it. Wasn't paying attention >_<

Well, time to get this fixed. Sorry, jatinderalagh.
0
 
SonicX_BlueBlurAuthor Commented:
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.
0
 
jatinderalaghCommented:
Thanks

;)
JAtinder
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 5
  • 5
  • 3
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now