How can I determine in KeyDown
that CtrlUp was pressed.
private void listView1_KeyDown(object sender, KeyEventArgs e)
{
this will work for sure. Be careful to handle KeyUp
event and not keyDown
.
private void mainForm_KeyUp(object sender, KeyEventArgs e)
{
if (e.Modifiers == Keys.Control && e.KeyCode == Keys.A)
{
//insert here
}
}
For me, keyDown
didn't work, keyU
p worked instead for the same code.
I don't know why, but it seems because keyDown
event happens directly after you press any key, even if that was ctrl key, so if you pressed ctrl+Up you will press ctrl key before the UP key and thus the event will occur before you can press the other, also pressing the second key will triggers the event again.
While using KeyUp
will not trigger the event until you release the key, so you can press ctrl, and the press the second key, which will trigger one event.
You can try using the Keyboard
object to detect the IsKeyDown
property. Also, if you don't want the browser shortcut to over-ride you can set Handled
property to true.But be careful when over-riding browser shortcuts as it could cause confusion.
private void Page_KeyDown(object sender, KeyEventArgs e)
{
// If leftCtrl + T is pressed autofill username
if (Keyboard.IsKeyDown(Key.T) && Keyboard.IsKeyDown(Key.LeftCtrl))
{
txtUser.Text = "My AutoFilled UserName";
e.Handled = true;
}
}
I tested below code. it works...
private void listView1_KeyDown(object sender, KeyEventArgs e)
{
if ((int) e.KeyData == (int) Keys.Control + (int) Keys.Up)
{
MessageBox.Show("Ctrl + Up pressed...");
}
}
private void listView1_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Up && Keyboard.IsKeyDown(Key.LeftCtrl))
{
//do stuff
}
}
This code will work only if you press first LeftCtrl, then "UP". If order has no importance, I recommend that one :
if ((Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl))&& Keyboard.IsKeyDown(Key.Z))
{
//do stuff
}
In that case, both Ctrl are taken in account, and no importance about the order.
From the MSDN page on KeyEventArgs:
if (e.KeyCode == Keys.F1 && (e.Alt || e.Control || e.Shift))
{
//Do stuff...
}
The KeyDown event will only hold the information for the most recent key that was pressed. I have had success building a string that contains all the most recent key down keys concatenated together.
If a key down of "Control" was clicked, or the strings becomes greater than 10 chars long, I clear the string.
Checking the string each time, then performing a task afterwords gives a great use for a secret hotkey function within your application.
private void ConfigurationManager_KeyDown(object sender, KeyEventArgs e)
{
string currentKey = e.KeyCode.ToString().ToLower();
if ((currentKey == "controlkey") || (hotKeyList.Length > 10))
{
hotKeyList = "";
}
else
{
hotKeyList += currentKey;
}
if ((hotKeyList == "int") && (!adminLogin))
{
hotKeyList = "";
adminLogin = true;
AdminLoginEvn();
}
}
For the function above my hotkeys would be "Control (clears the string) + i + n + t" to fire my AdminLoginEvn method. The adminLogin boolean was incorporated so I only run my AdminLoginEvn one time while the application is open.