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.
Related
In my C# app, i want to detect when the user presses the context menu button in keyboard. This button is on right side of key board in between the Windows and Ctrl keys.
Also need to figure out when user presses Shift + F10, which is another short cut for showing context menu.
How can i do that?
I am capturing the key presses inside a function.
private void keypressed(Object obj, KeyPressEventArgs e)
This code will display a number for any key combination you type in the form's header line. Just find out the corresponding number (for shift+F10 it is 65657) and react within a switch. Context Menu is 93. Some keys could differ with the keyboard layout...
Just paste this in any windows-form-class you like:
protected override bool ProcessCmdKey(ref Message msg, Keys keyData) {
int KeyAsInt=(int)keyData;
this.Text = KeyAsInt.ToString();
switch (KeyAsInt) {
case 65657:
MessageBox.Show("Heureka!");
break;
}
return base.ProcessCmdKey(ref msg, keyData);
}
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);
}
Is there anyway to check if a menu item event is coming from a click in the menu or from a short cut key being pressed?
I've tried adding event handlers to the key press and key down events, however these events aren't being "fired" when it's a shortcut key that's pressed (They do work as expected when it's not a shortcut key). I couldn't find anything in the sender object that was different between the menu click or shortcut click.
You can catch all key combinations by overriding ProcessCmdKey:
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (keyData == (Keys.Control | Keys.F))
{
Console.WriteLine("My code ran from shortcut");
myFunction();
}
return base.ProcessCmdKey(ref msg, keyData);
}
private void ToolStripMenuItem_click(object sender ...)
{
Console.WriteLine("My code ran from menu item");
myFunction();
}
void myFunction()
{
//your functionality here
}
Well to get help, you should post what you have tried. (Your source)
You can use a enum for this:
enum Sender
{
Shortcut,
Menu
}
void MenuEvent(Sender sender)
{
if (sender == Sender.Shortcut)
{
}
else
{
}
}
//if you click from the menu
void btnMenuClick()
{
MenuEvent(Sender.Menu);
}
//if you use shortcut
void OnShortcutEvent()
{
MenuEvent(Sender.Shortcut);
}
Edit: I guess my answer was to vague so I edited the code. I hope its more clear now, but I must say the OP should give more details as well, such as posting some code.
I see a single solution to this problem - override the ToolStripMenuItem's ProcessCmdKey method which is raised when a shortcut is processed. In this case, you can determine when a click was caused by a shortcut. Obviously, you need to use your own class of the ToolstripMenuItem instead of the standard one.
Handle the MouseDown event to process your mouse-click.
menuItem.MouseDown += new MouseEventHandler(Process_Mouse_Click_Handler);
Handle the Click event to process your shortcut.
menuItem.Click+= new EventHandler(Process_Shortcut_Handler);
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 am implementing a search function in a windows form in c#. I have set KeyPreviewto true on the form and have added an event handler for KeyDown so I can catch things like ctrl+f, esc and enter.
I am catching these keys just fine and I'm able to make my text box appear, but I am unable to type into the box. All of the keys are going to PortsTraceForm_KeyDown(...) but they never make it to the text box. According to the msdn page about KeyPreview, setting e.Handled to false should cause the event to pass to the view in focus (the text box), but this isn't happening. I have not registered a KeyDown event for the text box, so it should be using the default behavior. Have I missed something?
KeyDown event:
private void PortsTraceForm_KeyDown(object sender, KeyEventArgs e)
{
e.SuppressKeyPress = true;
e.Handled = false;
if (e.KeyData == (Keys.F | Keys.Control)) // ctrl+f
{
e.Handled = true;
ShowSearchBar();
}
else if (e.KeyCode == Keys.Escape) // esc
{
e.Handled = true;
HideSearchBar();
}
else if (e.KeyCode == Keys.Enter) // enter
{
if (searchPanel.Visible)
{
e.Handled = true;
if (searchShouldClear)
SearchStart();
else
SearchNext();
}
}
}
show search bar:
private void ShowSearchBar()
{
FindBox.Visible = true;
FindBox.Focus(); // focus on text box
}
hide search bar:
private void HideSearchBar()
{
this.Focus(); // focus on form
FindBox.Visible = false;
}
Your TextBox likely does not have focus even though you are calling Focus(). From the documentation:
Focus is a low-level method intended primarily for custom control authors. Instead, application programmers should use the Select method or the ActiveControl property for child controls, or the Activate method for forms.
You can check the return value of Focus() for success, but I have had little luck in the past using that method to set focus to an arbitrary control. Instead, try using the method that the documentation suggests, i.e., call Select().
EDIT:
Nevermind (though it's still valid advice), I think I see your problem:
e.SuppressKeyPress = true
Why are you doing this? Again, from the docs:
[SuppressKeyPress] Gets or sets a value indicating whether the key event should be passed on to the underlying control
So you are intentionally preventing the TextBox from getting key events. If you want to pass the event through you shouldn't be setting that property to false.
try this example , of overrides method.
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
// your code here
// this is message example
MessageBox.Show(keyData.ToString());
return base.ProcessCmdKey(ref msg, keyData);
}
Regards.