Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Keypress event in Windows Panel control in C#

i want to get keypress event in windows panel control in c#, is any body help for me...

like image 418
ratty Avatar asked Jan 25 '10 12:01

ratty


People also ask

How do you KeyPress an event?

The keypress event is fired when a key that produces a character value is pressed down. Examples of keys that produce a character value are alphabetic, numeric, and punctuation keys. Examples of keys that don't produce a character value are modifier keys such as Alt , Shift , Ctrl , or Meta .

What is KeyPress event in C#?

Key Press EventThis event fires or executes whenever a user presses a key in the TextBox. You should understand some important points before we are entering into the actual example. e. Keychar is a property that stores the character pressed from the Keyboard.

Which event is called when key is pressed?

The keydown event is fired when a key is pressed. Unlike the deprecated keypress event, the keydown event is fired for all keys, regardless of whether they produce a character value. The keydown and keyup events provide a code indicating which key is pressed, while keypress indicates which character was entered.

How can I listen for KeyPress event on the whole page?

For this, you have to import HostListener in your component ts file. import { HostListener } from '@angular/core'; then use below function anywhere in your component ts file. @HostListener('document:keyup', ['$event']) handleDeleteKeyboardEvent(event: KeyboardEvent) { if(event.


3 Answers

You should handle the Panel.KeyPress event.

Example

public void MyKeyPressEventHandler(Object sender, KeyPressEventArgs e)
{
    ... do something when key is pressed.
}

...

(MyPanel as Control).KeyPress += new KeyPressEventHandler(MyKeyPressEventHandler);
like image 130
James Avatar answered Oct 03 '22 20:10

James


The problem is, that at first your main form got the KeyPress and will immediately send this message to the active control. If that doesn't handle this key press it will be bubbled up to the parent control and so on.

To intercept this chain, you have to in your Form.KeyPreview to true and add an handler to Form.KeyPress. Now you can handle the pressed key within your form.

like image 37
Oliver Avatar answered Oct 03 '22 21:10

Oliver


"Panel" objects cannot receive the "KeyPress" event correctly.

I've created Panel overload:

public class PersoPanel : Panel

and used the overridden method ProcessCmdKey:

protected override bool ProcessCmdKey(ref Message msg, Keys keyData)

to intercept pressed keys:

protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
    MessageBox.Show("You press " + keyData.ToString());

    // dO operations here...

    return base.ProcessCmdKey(ref msg, keyData);
}
like image 21
Picsdonald Avatar answered Oct 03 '22 20:10

Picsdonald