This question already has an answer here:
Capture Key Sequence via ProcessCmdKey
(1 answer)
Closed 7 years ago.
I need to make a shortcut key combination of three letters (Ctrl+L+I)
I tried a lot to do so but no luck .
I tried it in this way
private void MDIParent2HOME_KeyDown(object sender, KeyEventArgs e)
{
if (e.Control && e.Keycode==Keys.L && e.KeyCode == Keys.I)
{//login
Form1 chilform = new Form1();
chilform.MdiParent = this;
chilform.Show();
}
}
but this didn't work.
then I changed my key combination (ctrl+ALt+L) and tried it in same way
private void MDIParent2HOME_KeyDown(object sender, KeyEventArgs e)
{
if (e.Control && e.Alt && e.KeyCode == Keys.L)
{//login
{
Form1 chilform = new Form1();
chilform.MdiParent = this;
chilform.Show();
}
}
}
and I am wondering that worked perfect .I couldn't get the reason do anyone know about this behaviour of KeyDown event.Also help me if I can do the same with (ctrl+L+I) . Thanks
Hmmm...I think my solution works
private bool IfSeen;
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (IfSeen)
{
if (keyData == (Keys.Control | Keys.I))
{
MessageBox.Show("You pressed Ctrl+L+I");
}
IfSeen= false;
return true;
}
if (keyData == (Keys.Control | Keys.L))
{
IfSeen= true;
return true;
}
return base.ProcessCmdKey(ref msg, keyData);
}
You are checking for e.Keycode==Keys.L && e.KeyCode == Keys.I. I think e.Keycode contains only the value of a single key, L or I, but not both at the same time, so your check will always fail.
Note that Alt, Shift and Ctrl are modifiers and are handled a bit differently than other keys.
Related
I have a class that catches keyboard keys and I want to catch a specific combination:
Alt + 1
And in case this combination is detected to do my stuff.
This is what I have try:
private bool isAltPressed;
private bool isOnePressed;
private bool bothPressed;
private void HookManager_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
if (e.KeyCode == Keys.LMenu)
isAltPressed = true;
if (e.KeyCode == Keys.D1)
isOnePressed = true;
if (isAltPressed & isOnePressed)
bothPressed = true;
}
private void HookManager_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e)
{
if (bothPressed)
// Do something...
if (e.KeyCode == Keys.LMenu)
{
isAltPressed = false;
bothPressed = false;
}
if (e.KeyCode == Keys.D1)
{
isOnePressed = false;
bothPressed = false;
}
}
So where do I need to verify that both keys are pressed and released and then do my stuff?
Try this:
if ( e.KeyData == (Keys.Alt | Keys.D1) )
Keys is a struct having a flag attribute.
It means that several key codes can be combinated with the logic or operator to form the result.
[Flags]
public enum Keys ...
You should not wait for alt key to be released to perform your action. Unless you can explain why you need to make sure both the alt key and the key pressed must be released before 'doing something', the following code should suffice in either your KeyUp or KeyDown event.
if (e.Modifiers == Keys.Alt && e.KeyCode == Keys.D1)
{
// Do Something
}
I'm designing a device app. Compact Framework 2.0
I want the user to press F1 to navigate to the next screen, but it does not work.
Can't seem to find a solution.
Is it possible?
This is how I normally use Keypress:
This works:
if (e.KeyChar == (char)Keys.M)
{
MessageBox.Show("M pressed");
e.Handled = true;
}
This dos NOT work:
if (e.KeyChar == (char)Keys.F1)
{
MessageBox.Show("F1 pressed");
e.Handled = true;
}
try this
private void Form1_Load(object sender, EventArgs e)
{
this.KeyPreview = true;
this.KeyDown += new KeyEventHandler(Form1_KeyDown);
}
void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode.ToString() == "F1")
{
MessageBox.Show("F1 pressed");
}
}
Refer This
You can override the ProcessCmdKey method of your form class and use keyData == Keys.F1 to check whether F1 is pressed. Above link has example as follows.
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (keyData == Keys.F1)
{
MessageBox.Show("You pressed the F1 key");
return true; // indicate that you handled this keystroke
}
// Call the base class
return base.ProcessCmdKey(ref msg, keyData)
}
Some keys(like f1,f2,arrow keys ,tab ....)cannot be "captured" by keychar for that you need to use keycode:
if (e.KeyCode == Keys.F1)
{
// do stuff
}
keychar property - http://msdn.microsoft.com/en-us/library/system.windows.forms.keypresseventargs.keychar.aspx
Use KeyDown instead of KeyPress:
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyData == Keys.F1)
{
// your code here
}
}
Also set KeyPreview to true
I have a simple form by which I take input:
12 Buttons, 1 Textbox (disabled & read-only)
this is what I do to handle input
Login_KeyDown() is common method I call for all the KeyDown of every UI component & the form itself..
private void Login_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Escape)
{
Application.Exit();
}
else if (e.KeyCode == Keys.NumPad9 || e.KeyCode == Keys.D9)
{
button3.BackgroundImage = Properties.Resources.button_hover;
button3.ForeColor = Color.White;
pin.Text = pin.Text + "9";
}
else if (e.KeyCode == Keys.Back)
{
button11.BackgroundImage = Properties.Resources.button_hover;
button11.ForeColor = Color.White;
if (pin.Text.Length > 0)
pin.Text = pin.Text.Substring(0, pin.Text.Length - 1);
}
else if (e.KeyCode == Keys.Enter)
{
MessageBox.Show(pin.Text);
}
}
This code works fine when I start the app but after I have clicked on any component, rest of the code works fine but "Enter Key Condition" doesn't work.
My guess is as "Enter Key Condition" is not working for UI components or something like that.
I have also tried using "Key Press Event" which uses KeyPressEventArgs then checking KeyChar == 13 but that is also not working.
What is the problem, and how can I solve it?
p.s.
I have not set any button click events for any button, the app is 100% KBoard based.
Check out PreviewKeyDown. Return raises that event on button controls.
private void Form1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
if (e.KeyCode == Keys.Return)
MessageBox.Show("I found return");
}
Or alternatively you can force it to raise those special keys in the KeyDown Event by using:
private void Form1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
if (e.KeyCode == Keys.Return)
e.IsInputKey = true;
}
More information: http://msdn.microsoft.com/en-us/library/system.windows.forms.control.previewkeydown.aspx
Have you tried to use
Keys.Return
Instead
Edit:
Just thought of this. Do you have the acceptbutton set for the main form?
This is because your Form has AcceptButton defined. For example, you have a "OK", "Accept" or "Confirm" button with DialogResult set to "OK". This tells its parent form that there is an AcceptButton, and the Enter event on the form would go to this button.
What you should do is to catch the Enter key at form level. Add this code to the form:
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if ((this.ActiveControl == myTextBox) && (keyData == Keys.Return))
{
//do something
return true;
}
else
{
return base.ProcessCmdKey(ref msg, keyData);
}
}
I am trying to bind multiple keys on a KeyDown event to change a bool variable, but I can't seem to figure out how to trigger the asterisk/star key (*) with the Left Shift key in the following code:
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (keyData == Keys.Multiply || keyData == (Keys.LShiftKey | Keys.OemQuotes))
{
Valgt = true;
}
}
This answer will not be keyboard layout invariant but this would do the trick on a US-EN keyboard. It's not robust but can be adapted to your local layout.
if (keyData == Keys.Multiply || keyData == (Keys.Shift | Keys.D8))
{
Valgt = true;
}
Alternatively you can use Control_KeyPress event
private void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == '*')
{
Valgt = true;
}
}
i thought i solved this problem by myself but it came back to haunt my application so here it goes:
i have the following keydown event handler registered in a form with a couple of disabled and readonly textboxes and they are only simple shortcuts for the buttons:
private void AccountViewForm_KeyDown(object sender, KeyEventArgs e)
{
//e.SuppressKeyPress = true;
//e.Handled = true;
if (Control.ModifierKeys == Keys.Control && e.KeyCode == Keys.E && !isInEditMode)
btnEditMode_Click(sender, e);
if (Control.ModifierKeys == Keys.Control && e.KeyCode == Keys.S && isInEditMode) btnEditMode_Click(sender, e);
if (e.KeyCode == Keys.Escape) btnCancel_Click(sender, e);
if (Control.ModifierKeys == Keys.Control && e.KeyCode == Keys.W) Close();
}
the form has KeyPreview set to true but whenever a readonly textbox has focus and i press Ctrl + E i can't get "Control.ModifierKeys == Keys.Control" and "e.KeyCode == Keys.E" to be both true at the same time. What is really strange is that Ctrl + W works. Anyone has any idea what the hell is going on? :(
According to this question and this one, It looks like a more general way to handle keyboard shortcuts is to override the ProcessCmdKey() method:
protected override bool ProcessCmdKey(ref Message msg, Keys keyData) {
if (keyData == (Keys.Control | Keys.F)) {
MessageBox.Show("What the Ctrl+F?");
return true;
}
return base.ProcessCmdKey(ref msg, keyData);
}
Have you considered using Alt + E and Alt + S and just setting the mnemonic property for your buttons? That seems to work well for me, and it's easier to set up.
I had the same problem
In my application the shortcuts did not work if the form was opened with an invoked show(). They did work if the form was opened with ShowDialog(). I also found that the keydown event for the textbox was not fired by CTRL+C etc but strangely was fired by CTRL+B.
The workaround fix involved using the keyup event instead of the keydown.
Here's my code :
public void ShortCut(object sender, KeyEventArgs e, TextBox box )
{
string s, tmp1, tmp2;
int selectionIndex;
switch (e.KeyCode)
{
case Keys.V: // paste
if (Clipboard.ContainsText())
{
s = Clipboard.GetText(TextDataFormat.Text);
selectionIndex = box.SelectionStart;
tmp1 = box.Text.Substring(0, selectionIndex);
tmp2 = box.Text.Substring(selectionIndex + box.SelectionLength);
box.Text = tmp1 + s + tmp2;
}
break;
case Keys.C: // copy
if (box.SelectionLength > 0)
{
selectionIndex = box.SelectionStart;
s = box.Text.Substring(selectionIndex, box.SelectionLength);
Clipboard.SetText(s);
}
break;
case Keys.X: // cut
if (box.SelectionLength > 0)
{
selectionIndex = box.SelectionStart;
s = box.Text.Substring(selectionIndex, box.SelectionLength);
Clipboard.SetText(s);
tmp1 = box.Text.Substring(0, selectionIndex);
tmp2 = box.Text.Substring(selectionIndex + box.SelectionLength);
box.Text = tmp1 + tmp2;
}
break;
case Keys.A: // all
box.SelectAll();
break;
}
}
//and here's an example calling it :
private void textBoxExpression_KeyUp(object sender, KeyEventArgs e)
{
if (e.Control)
{
m_Host.ShortCut(sender, e, textBoxExpression);
}
else
{
....
}
}