i want to get keypress event in windows panel control in c#, is any body help for me...
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);
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.
"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);
}
Panel + Keypress - C# Discussion Boards - CodeProject
http://www.codeproject.com/Messages/704386/Panel-plus-Keypress.aspx
Related
I would like to retrieve pressed Keys in my UserControl.
The problem is that the keyboard notifications are posted to the control with the focus and this one is my windows form.
I already tried to focus the UserControl, but these approaches are not working.
For example:
protected override bool ProcessCmdKey(ref Message msg, Keys keyData) {
if (keyData == Keys.Escape) {
this.Visible = false;
return true;
}
return base.ProcessCmdKey(ref msg, keyData);
}
has no effect.
Source: UserControl KeyDown Event Not Fire Against Revoke Of Windows Application KeyDown Event
If I initialize my UserControl the TrappedKeyDown(KeyEventArgs e) Event is not firing.
I have a form with a text box.
myTB.KeyDown += new KeyEventHandler(this.myTB_KeyDown);
In this event I can get Keys.Apps to open the context menu.
Now another shortcut for context menu is Shift + F10.
Is it possible to capture that too inside the KeyDown event?
Any logic that I can implement to capture those keys?
So far what I see is, when the Shift key is pressed, that time itself the KeyDown event get's called and no way checking for both Shift and F10 together!
If I understand your question correctly, and you want to disable default Shift+F10 menu and handle those combination yourself, it's enough to handle KeyDown event and detect the combination and the set e.Handled=true;:
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
if(e.KeyCode== Keys.F10 && e.Shift==true)
{
//Shift + F10 pressed, do what you need.
e.Handled = true;
}
}
You can also disable the default context menu that will be shown, by setting ShortcutsEnabled property of your TextBox to false.
Here is a pretty safe way to check for shift + F10
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (keyData == (Keys.F10 | Keys.Shift))
{
if (txtBox1.Focused)
{
txtBox1.Text = "Captured!";
return true;
}
}
return base.ProcessCmdKey(ref msg, keyData);
}
Description:
This will capture every keystroke. From there is will check if the keystroke is Shift+F10, the single pipe acts like &&. After that it does a simple check to see if the textbox you are planning on having the event happen is focuses, or active control. If it fails either of those checks it sends the information of the keystrokes to return base.ProcessCmdKey(ref msg, keyData); which will give normal functionality to the keystroke without any overridden checks or whatever you may want to do with the captured keystrokes.
I'm using Winforms.
I've a screen approx. 10 fields. and a Update button.
But I don't want to use neither show on screen a button (btnUpdate).
I just want to show the the fields, they can change some values and by pressing the enter it should execute a function in code behind.
I googled and find some solutions like KeyPress on TextBox or whatever, but I don't want to link this to a TextBox. Then I found form.Acceptbutton = btnUpdate... but then I have to use a button on my designer.
so how can I make a situtation by not USING a Button control to do an update (in other words executing function in code-behind by pressing the Enter Key).
Try overriding the ProcessCmdKey
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (keyData == Keys.Return)
{
//Raise Update Event
return true;
}
else if (keyData == Keys.Escape)
{
//Raise Cancel Event
return true;
}
return base.ProcessCmdKey(ref msg, keyData);
}
I've created custom button derived from a normal .Net button and have added the following property to add a short cut key combination:
public Keys ShortCutKey { get; set; }
I want this combination to fire the click event of the button but have no idea how to implement this when the button is placed on a form. I know the standard way of doing a button shortcut is to use the & before the short cut character but I need to use a key combination.
Any ideas?
Many Thanks
Override the form's ProcessCmdKey() method to detect shortcut keystrokes. Like this:
private bool findShortCut(Control.ControlCollection ctls, Keys keydata) {
foreach (Control ctl in ctls) {
var btn = ctl as MyButton;
if (btn != null && btn.ShortCutKey == keydata) {
btn.PerformClick();
return true;
}
if (findShortCut(ctl.Controls, keydata)) return true;
}
return false;
}
protected override bool ProcessCmdKey(ref Message msg, Keys keyData) {
if (findShortCut(this.Controls, keyData)) return true;
return base.ProcessCmdKey(ref msg, keyData);
}
Where MyButton is assumed to be your custom button control class.
I'm assuming you are using WinForms, given that the ampersand character is used in WinForms control captions to denote the shortcut character. If that is the case, then you can use the Button.PerformClick() method on a WinForms Button in order to fire the Click event manually.
If this is not the case and you are, in fact, using WPF; then take a look at the link Dmitry has posted in his comment for WPF Input Bindings.
I have a custom control MyLookUpEdit, it inherites Devexpress LookUpEdit.
In MyLookUpEdit, I have below method:
protected override bool ProcessDialogKey(Keys keyData)
I use this custom control (let's call it nameLookUpEdit) on a form. When I tab through the control, I see ProcessDialogKey is called and it focus to next control according to tabIndex.
Now we hope when enter is pressed, our control can behavior exactly the same as tab pressed. I think I should add in MyLookUpEdit one of those methods:
protected override void OnKeyDown(KeyEventArgs e)
or
protected override void OnEditorKeyDown(KeyEventArgs e)
and capture the enter key and hence call the ProcessDialogKey explicitly:
{
if(e.KeyData == Keys.Enter)
{
ProcessDialogKey(Keys.Tab);
}
base....
}
But when I test on it, I find when nameLookUpEdit is currently focused, I press enter key, neither onKeyDown nor onEditorKeyDown will be called.
Why this happens?
And what is the correctly way to let enter behavior the same as tab?
try overriding ProcesscmdKey ?
edit: Added the return statement for correctness.
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (keyData == Keys.Enter)
{
//do stuff
}
return base.ProcessCmdKey(ref msg, keyData);
}