Catch Alt + other key shortcut - c#

I have to catch user's input to send a shortcut to my WPF application.I found on internet that I have to do something like this: Catch when a key is pressed:
void keyDown(object sender, KeyEventArgs e)
{
if (Keyboard.Modifiers.HasFlag(Modifiers.Shift))
KeyPressed.SetShift(true);
if (Key.Shift != e.Key && Key.LeftAlt != e.Key && ....)
KeyPressed.SetKey(e.Key);
}
where KeyPressed is a class with static boolean variables to catch if ⇧Shift, Alt or Ctrl and another key are pressed (with Alt and Ctrl instead of ⇧Shift in the if clause). The second if is to catch a key different from Alt, ⇧Shift, Control for the shortcut. For example, for the shortcut Alt+C we have:
KeyPressed.Shift = false;
KeyPressed.Alt = true;
KeyPressed.Ctrl = false;
KeyPressed.key = Key;
Where the last element is of type System.Window.Input.Key.Catch when a key is released:
void keyUp(object sender, KeyEventArgs e)
{
if (KeyPressed.getShift())
this.textField.Text += "+Shift";
if (KeyPressed.getKeyCode())
this.textField.Text += "+" + KeyPressed.k.toString();
KeyPressed.SetShift(false);
}
and here simply I append to a textField the input received, after that I set all keys to false to catch the next shortcut correctly. This code works fine for all shortcuts like Ctrl+A, Ctrl+Alt+C, ⇧Shift+L, Alt, but when I press the shortcut like Alt+V, it catchs only Alt, not the other key.
How can I manage this? Is there a way to handle shortcuts in a better manner?

You need to get the actual key when in case of a SystemKey (Alt etc), you can use this helper function to get the real key behind the system key.
public static Key RealKey(this KeyEventArgs e)
{
switch (e.Key)
{
case Key.System:
return e.SystemKey;
case Key.ImeProcessed:
return e.ImeProcessedKey;
case Key.DeadCharProcessed:
return e.DeadCharProcessedKey;
default:
return e.Key;
}
}
You could check my answer here for more info.

Store the Alt-modifier state in a local variable. I'm unsure of the reasons why but this made it work for me.
private bool _altModifierPressed = false;
private void Window_KeyDown(object sender, KeyEventArgs e)
{
_altModifierPressed = (Keyboard.IsKeyDown(Key.LeftAlt) || Keyboard.IsKeyDown(Key.RightAlt));
if (_altModifierPressed && Keyboard.IsKeyDown(Key.V))
{
// code to handle Alt + V
}
}
UPDATE:
Alternatively, you could do something like this (no need for local variable)
if (((Keyboard.Modifiers & ModifierKeys.Alt) == ModifierKeys.Alt) && Keyboard.IsKeyDown(Key.V))
{
// code to handle Alt + V
}
But I noticed that with either approach (since the enum has the Flag attribute) any combination of keys including Alt & V will work. So both execute if I for example press Alt+G+V. Good luck.

If you want to use [Alt + A] in KeyboardHook in Office VSTO, this is how it's used.
if (IsKeyDown(Keys.Menu) &&
keyData == Keys.A &&
KeyWasAlreadyPressed == false &&
!IsKeyDown(Keys.Controlkey) &&
!IsKeyDown(Keys.ShiftKey))
{
//Enter your code here
}
Note:
Key.Menu denotes Alt Keys
Also condition says, Alt+A (and do not invoke when control or shift key is pressed in addition to Alt + A)

switch (e.Key)
{
case Key.System:
if (((KeyboardEventArgs)e).KeyboardDevice.Modifiers == ModifierKeys.Alt)
{
if (e.SystemKey == Key.Left)
moiveVideoPsition(-30);
else if (e.SystemKey == Key.Right)
moiveVideoPsition(30);
}
break;
This work well for me

Related

Forms, basic calculator key triggering problem

private void UserInputText_KeyDown(object sender, KeyEventArgs e)
{
if ((e.KeyCode == Keys.D4 && e.Modifiers == Keys.Shift) || (e.KeyCode == Keys.Add))
{
if (String.IsNullOrEmpty(UserInputText.Text))
{
MessageBox.Show("Bir sayı giriniz.");
UserInputText.Clear();
return;
}
if (double.TryParse(UserInputText.Text, out sayı1))
{
CalculationResultText.Text = sayı1 + " + ";
islem = "+";
UserInputText.Clear();
}
else
{
MessageBox.Show("Sadece sayı değeri girebilirsiniz.");
UserInputText.Clear();
}
}
}
I am coding a basic forms calculator. I am trying to trigger addition function and clear the textbox when textbox is focused and user presses "+" key. "if (String.IsNullOrEmpty(UserInputText.Text)) and else conditions work well. But if no Message boxes shows up as in the
if (double.TryParse(UserInputText.Text, out sayı1)) condition, the "+" character remains in the textbox as in the image. Thanks for help.
If I understand correctly, you want to first check the character that was typed in and if it's incorrect then you want to prevent this character from appearing?
If so, then you need to set e.Handled = true property when you want to prevent it.
This call tells the GUI element (your TextBox) that "I did all the checks for this event (i.e. KeyDown event), and I don't want you to contribute in handling of this event (i.e. normally the TextBox would try to add this character to its Text property, but you prevent it)".
Check out documentation on KeyEventArgs.Handled.
KeyPress event enables you to prevent any further changes in the TextBox.
You can do that thanks to Handled property of KeyPressEventArgs
private void UserInputText_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == '+')
{
UserInputText.Clear();
e.Handled = true;
}
}

Disable Ctrl or Alt key without registry using c#

I got a question about KeyCode and disabling special keys. I know this question was asked a few times, but I didn't find an answer I can use and which works so I came here to ask :)
I'm writing a program which blocks every key or key combinations (like Alt+F4 etc.). The application is not for me, it's for customers which only be able to navigate in this program. This all works fine, but I can't disable Left CTRL, Right CTRL or Alt key. I got this code for try blocking these keys:
private void webBrowser1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
if (e.KeyCode == Keys.LControlKey)
{
MessageBox.Show("LCtrl", "Warnung", MessageBoxButtons.OK);
}
else if (e.KeyCode == Keys.RControlKey)
{
MessageBox.Show("RCtrl", "Warnung", MessageBoxButtons.OK);
}
else if (e.KeyCode == Keys.Alt)
{
MessageBox.Show("Alt", "Warnung", MessageBoxButtons.OK);
}
else if (e.KeyCode == Keys.Delete)
{
MessageBox.Show("Delete", "Warnung", MessageBoxButtons.OK);
}
}
I only use MessageBox.Show(); that I can see if it works. Delete key works fine, but the other one not. Is it possible to do this without editing the registry and for Win7? Does anyone know why or can give me a hint?
Cheers
EDIT: I block all other keys in this way:
Blocking shortcut keys using c#
Disclaimer: I'm not highly experienced in user input classes, but here's my input.
CTRL and ALT are examples of modifier keys. That is to say, they modify other (non-modifier) keys to create a key combination. Your UI is likely only able to pick up a complete key combination. For example:
private void keyPressed(object sender, PreviewKeyDownEventArgs e)
{
e.KeyCode == Key.A; // True (pressed A)
e.KeyCode == Key.Control; // False (no key pressed)
e.Modifiers == Keys.Control; // True (user is pressing the modifier CTRL)
e.KeyCode == Key.A && e.Modifiers == Keys.Control; (pressed key A with modifier CTRL)
}
As for disabling the key, you could just catch e.Modifiers:
private void ignoreCtrl(object sender, PreviewKeyDownArgs e)
{
if (e.Modifiers != Keys.Control) { /* Pass to handler */ }
else { /* Discard */ }
}
Again, I'm not experienced in your particular framework but this would be my guess. I used the following SO sources:
How to use multiple modifier keys in C#
Determine whether modifier key was pressed
This should be a comment because I have not tested it, but I need some code as example so I write here. You tell me if it works or not.
The PreviewKeyDownEventArgs contains other properties that you can use to check if one or more modifier keys are pressed.
You can then try to set the IsInputKey property to false to prevent further processing for whatever regular key has been pressed together the modifier key.
private void webBrowser1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
if (e.Control)
{
MessageBox.Show("Ctrl", "Warnung", MessageBoxButtons.OK);
e.IsInputKey = false;
}
else if (e.Alt)
{
MessageBox.Show("Alt", "Warnung", MessageBoxButtons.OK);
e.IsInputKey = false;
}
Have you tried checking the modifier keys? Such as below.
Note this is for WPF as you didnt state win forms or wpf.
if ((Keyboard.Modifiers & ModifierKeys.Control) == ModifierKeys.Control)
{
MessageBox.Show("Control Button Down");
}
else if ((Keyboard.Modifiers & ModifierKeys.Alt) == ModifierKeys.Alt)
{
MessageBox.Show("Alt Down");
}
Cheers.

Detect Shift+1 as Key.Add

I would like to detect the user pressing the "add" key in the .net 4 WPF KeyDown event handler. To do this I use the following test:
if (e.Key == Key.Add)
This doesn't detect the case when the user presses Shift+1 (which corresponds to "add" on my keyboard layout).
How can I detect this? I'm not convinced that testing
if (e.Key == Key.D1 && Keyboard.Modifiers == ModifierKeys.Shift)
is the right solution as it may be mapped elsewhere on another keyboard layout.
Any suggestions?
You might consider using the KeyPress event handler instead.
private void trackBarFrames_KeyDown(object sender, KeyEventArgs e)
{
switch ( e.KeyCode)
{
case Keys.Add :
// Nummeric Keypad Add
AddSomething();
break;
case Keys.Oemplus :
// Regular keyboard Add
// OemPlus is assigned to the regular keyboard key with a "Add" Sign but doesn not take shift conditions in account..!
if (e.Modifiers == Keys.Shift)
{
AddSomething();
}
break;
}
}

KeyDown event - how to easily know if the key pressed is numeric?

I am currently handling the KeyDown event of a DataGridView control.
One of the columns is filled by calculated values and I want the user to be able to override the cell value if they want.
When the user presses a numeric key, the cell goes into EditMode and allows the user to override the value. If the key is not numeric, nothing happens...
That is working pretty well... the problem is that I find the code for it ugly...
I can't seem to find a neat way to handle all the numeric keys in a single condition, so I've made a switch case construct to deal with all the possible numeric keys, like this:
switch (e.KeyCode)
{
case Keys.D0:
case Keys.D1:
case Keys.D2:
case Keys.D3:
case Keys.D4:
case Keys.D5:
case Keys.D6:
case Keys.D7:
case Keys.D8:
case Keys.D9:
case Keys.Decimal:
case Keys.NumPad0:
case Keys.NumPad1:
case Keys.NumPad2:
case Keys.NumPad3:
case Keys.NumPad4:
case Keys.NumPad5:
case Keys.NumPad6:
case Keys.NumPad7:
case Keys.NumPad8:
case Keys.NumPad9:
[code to make the cell go to editMode, etc...]
Sure, it works, but there has to be a better and shorter way, right?
All I could find using Google is converting e.KeyCode to a char, but when using numeric keys, it goes gives letters even for the numeric values...
Thanks.
Try
if ((e.KeyCode >= Keys.D0 && e.KeyCode <= Keys.D9) ||
(e.KeyCode >= Keys.NumPad0 && e.KeyCode <= Keys.NumPad9) ||
e.KeyCode == Keys.Decimal)
{
// Edit mode
}
If you use the KeyPress event, the event signature has a KeyPressEventArgs with a KeyChar member that gives you the character for the numberpad keys. You can do a TryParse on that to figure out if its a number or not.
private void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
int i;
if (int.TryParse(e.KeyChar.ToString(), out i))
{
MessageBox.Show("Number");
}
}
Sorcerer86pt's solution was the simplest, however, when a user presses a control key, like backspace, then it breaks. To solve that problem, you can use the following snippet:
void KeyPress(object sender, KeyPressEventArgs e)
{
if(!Char.IsNumber(e.KeyChar) && !Char.IsControl(e.KeyChar))
{
//The char is not a number or a control key
//Handle the event so the key press is accepted
e.Handled = true;
//Get out of there - make it safe to add stuff after the if statement
return;
}
//e.Handled remains false so the keypress is not accepted
}
If you're using WPF, you might find that a TextBox doesn't have a KeyPressed event. To fix this, I used the following code.
void ValidateKeyPress(object sender, KeyEventArgs e)
{
char keyPressed = WPFUtils.Interop.Keyboard.GetCharFromKey(e.Key);
if (!Char.IsNumber(keyPressed) && !Char.IsControl(keyPressed))
{
//As above
e.Handled = true;
return;
}
}
You may notice the weird function call WPFUtils.Interop.Keyboard.GetCharFromKey(e.Key) this is one of the useful functions I've collected.
You can find it here.
Why use keycodes, when you can use this:
void Control_KeyPress(object sender, KeyPressEventArgs e)
{
if (Char.IsDigit(e.KeyChar))
{
//do something
}
else
{
//do something else
}
}
It's cleaner and even if microsoft decides to change all enums vlue, it still would work
On the msdn help page they use this code in their example:
// Determine whether the keystroke is a number from the top of the keyboard.
if (e.KeyCode < Keys.D0 || e.KeyCode > Keys.D9)
...
// Determine whether the keystroke is a number from the keypad.
if (e.KeyCode < Keys.NumPad0 || e.KeyCode > Keys.NumPad9)
A bit more condensed version:
private void KeyPress(object sender, KeyPressEventArgs e)
{
e.Handled = !Char.IsDigit(e.KeyChar); // only allow a user to enter numbers
}
Just get the last char from the Key that will be number if a number was pressed.
This method works with KeyDown events not needing any other conditions.
Just call this static method and pass in the Key to check
public static bool IsNumber(Keys key)
{
string num = key.ToString().Substring(key.ToString().Length - 1);
Int64 i64;
if (Int64.TryParse(num, out i64))
{
return true;
}
return false;
}
void dataGridView1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
// Used this to find they key values.
//label1.Text += e.KeyValue;
// Check if key is numeric value.
if((e.KeyValue >= 48 && e.KeyValue <= 57) || (e.KeyValue >= 97 && e.KeyValue <= 105))
System.Console.WriteLine("Pressed key is numeric");
}

Detecting multiple simultaneous keypresses in C#

I am looking to emulate hyperterminal functionality for my Serial Communication in C# by detecting the keypresses of certain key combinations (escape sequences) which cannot be typed out such as Ctrl+C, Ctrl+Z, etc. I understand that these keys have their ASCII equivalents and can be transmitted as such. But I am facing problems with the detection of multiple keypresses. Some of my code is provided as a reference :
private void Transmitted_KeyDown(object sender, KeyEventArgs e)
{
if (e.Modifiers == Keys.Control || e.Modifiers== Keys.Shift || e.Modifiers==Keys.Alt)
{
var test = (char)e.KeyValue; // Only able to detect a single keypress!
ComPort.Write(test.ToString());
}
}
If you're looking for regular keys then you can store them in a list: On KeyDown, add the key to a list. On Key Up, remove it from the list. On KeyDown, check what's in the list.
However, I'm not sure that there are keydown/keyup events for modifier keys like ctrl, shift, alt. For those you can do something like this:
bool CtrlDown = ((e.Modifiers & Keys.Control) > 0);
bool CtrlOnlyModifierDown = ((e.ModifierKeys & Keys.Control) == Keys.Control)
e.KeyCode contains the key value + modifier info
e.KeyCode = e.KeyValue | e.Modifiers
Use e.KeyCode
Not sure if you have had any luck.
But try this code:
switch (e.KeyData)
{
case Keys.Control:
{
if (e.KeyData == Keys.Subtract)
{ }
else if (e.KeyData == Keys.C)
{ }
break;
}
}

Categories

Resources