hey I have a problem with my wpf webbrowser. I dont want that you can press shortcuts like "CRTL + N" for a new tab for example. I already found out how to do it, but if I want to handle more shortcuts it will only prevent the last one. I know that this will be very simple but I dont know how to fix it at the moment. Here is my code:
e.Handled = e.Key == Key.N && e.KeyboardDevice.Modifiers == ModifierKeys.Control;
e.Handled = e.Key == Key.O && e.KeyboardDevice.Modifiers == ModifierKeys.Control;
e.Handled = e.Key == Key.OemMinus && e.KeyboardDevice.Modifiers == ModifierKeys.Control;
e.Handled = e.Key == Key.OemPlus && e.KeyboardDevice.Modifiers == ModifierKeys.Control;
e.Handled = e.Key == Key.Subtract && e.KeyboardDevice.Modifiers == ModifierKeys.Control;
e.Handled = e.Key == Key.Add && e.KeyboardDevice.Modifiers == ModifierKeys.Control;
You need to OR together your conditions.
e.Handled = ((e.Key == Key.N) && (e.KeyboardDevice.Modifiers == ModifierKeys.Control)) ||
((e.Key == Key.O) && (e.KeyboardDevice.Modifiers == ModifierKeys.Control)) ||
((e.Key == Key.OemMinus) && (e.KeyboardDevice.Modifiers == ModifierKeys.Control)) ||
((e.Key == Key.OemPlus) && (e.KeyboardDevice.Modifiers == ModifierKeys.Control)) ||
((e.Key == Key.Subtract) && (e.KeyboardDevice.Modifiers == ModifierKeys.Control)) ||
((e.Key == Key.Add) && (e.KeyboardDevice.Modifiers == ModifierKeys.Control));
As Modifier CTRL appears to be common, this can be separated out from the keys & the simplified code would be something like
e.Handled = (e.KeyboardDevice.Modifiers == ModifierKeys.Control) &&
((e.Key == Key.N) || (e.Key == Key.O) || (e.Key == Key.OemMinus) || ...... )
Note that I have added brackets that some people will say are unnecessary, but I prefer them for readability.
Im trying block any number that dont be hex number, using Virtualkey and your codes
private void TxthexSoma1_KeyDown(object sender, KeyRoutedEventArgs e)
{
if (e.Key == (VirtualKey)(65) ||
e.Key == (VirtualKey)(66) ||
e.Key == (VirtualKey)(67) ||
e.Key == (VirtualKey)(68) ||
e.Key == (VirtualKey)(69) ||
e.Key == (VirtualKey)(70) ||
e.Key == (VirtualKey)(48) ||
e.Key == (VirtualKey)(49) ||
e.Key == (VirtualKey)(50) ||
e.Key == (VirtualKey)(51) ||
e.Key == (VirtualKey)(52) ||
e.Key == (VirtualKey)(53) ||
e.Key == (VirtualKey)(54) ||
e.Key == (VirtualKey)(55) ||
e.Key == (VirtualKey)(56) ||
e.Key == (VirtualKey)(57) ||
e.Key == (VirtualKey)(20) ||
e.Key == (VirtualKey)(8) ||
e.Key == (VirtualKey)(46))
{
e.Handled = false;
}
else
{
e.Handled = true;
}
But have some problems, for example: if the first lyric is uppercase happen nothing, and others characters like : (, ), #, ! and many others are accepets
how fixed this ?
I have the following code:
public void tbSpeed_KeyDown(object sender, KeyRoutedEventArgs e)
{
e.Handled = !((e.Key >= 48 && e.Key <= 57) || (e.Key >= 96 && e.Key <= 105) || (e.Key == 109) || (e.Key == 189));
}
Is there any way to detect if any modifier key like shift is being pressed ?
Use GetKeyState. e.g.
var state = CoreWindow.GetForCurrentThread().GetKeyState(VirtualKey.Shift);
return (state & CoreVirtualKeyStates.Down) == CoreVirtualKeyStates.Down;
Note: For Alt, you would use VirtualKey.Menu.
For Win10 UWP I noticed that the CTRL and SHIFT keys were set at Locked state. So I did the following:
var shiftState = CoreWindow.GetForCurrentThread().GetKeyState(VirtualKey.Shift);
var ctrlState = CoreWindow.GetForCurrentThread().GetKeyState(VirtualKey.Control);
var isShiftDown = shiftState != CoreVirtualKeyStates.None;
var isCtrlDown = ctrlState != CoreVirtualKeyStates.None;
You can try the following code
CoreVirtualKeyStates controlKeyState = Window.Current.CoreWindow.GetKeyState(VirtualKey.Control);
var ctrl = (controlKeyState & CoreVirtualKeyStates.Down) == CoreVirtualKeyStates.Down;
CoreVirtualKeyStates shiftKeyState = Window.Current.CoreWindow.GetKeyState(VirtualKey.Shift);
var shift = (shiftKeyState & CoreVirtualKeyStates.Down) == CoreVirtualKeyStates.Down;
Bitwise AND the Modifiers property of Keyboard with Shift Key -
bool isShiftKeyPressed = (Keyboard.Modifiers & ModifierKeys.Shift)
== ModifierKeys.Shift;
Try this too-
bool isShiftKeyPressed = (ModifierKeys & Keys.Shift) == Keys.Shift;
OR
Control.ModifierKeys == Keys.Shift
I am trying to disable all keystrokes entered into a text box except the following:
0 1 2 3 4 5 6 7 8 9 . (so all keys except the numbers and the '.' should be disabled)
Right now I have the following code but it only checks to see if a letter was entered as the first value (not to mention its really sloppy):
private void yDisplacementTextBox_TextChanged(object sender, EventArgs e)
{
if (yDisplacementTextBox.Text.ToUpper() == "A" || yDisplacementTextBox.Text.ToUpper() == "B" || yDisplacementTextBox.Text.ToUpper() == "C" ||
yDisplacementTextBox.Text.ToUpper() == "D" || yDisplacementTextBox.Text.ToUpper() == "E" || yDisplacementTextBox.Text.ToUpper() == "F" ||
yDisplacementTextBox.Text.ToUpper() == "G" || yDisplacementTextBox.Text.ToUpper() == "H" || yDisplacementTextBox.Text.ToUpper() == "I" ||
yDisplacementTextBox.Text.ToUpper() == "J" || yDisplacementTextBox.Text.ToUpper() == "K" || yDisplacementTextBox.Text.ToUpper() == "L" ||
yDisplacementTextBox.Text.ToUpper() == "M" || yDisplacementTextBox.Text.ToUpper() == "N" || yDisplacementTextBox.Text.ToUpper() == "O" ||
yDisplacementTextBox.Text.ToUpper() == "P" || yDisplacementTextBox.Text.ToUpper() == "Q" || yDisplacementTextBox.Text.ToUpper() == "R" ||
yDisplacementTextBox.Text.ToUpper() == "S" || yDisplacementTextBox.Text.ToUpper() == "T" || yDisplacementTextBox.Text.ToUpper() == "U" ||
yDisplacementTextBox.Text.ToUpper() == "V" || yDisplacementTextBox.Text.ToUpper() == "W" || yDisplacementTextBox.Text.ToUpper() == "X" ||
yDisplacementTextBox.Text.ToUpper() == "Y" || yDisplacementTextBox.Text.ToUpper() == "Z")
{
MessageBox.Show("Please enter a numeric value for the Y Displacement.", "Y Displacement: Numbers Only Error",
MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
Is there anyway to have it so when pressed, all of the keys on the keyboard (except the numbers and the period button) do not register (or disables) the actual value of the key and inputs nothing?
Use textBox1.KeyPress += textBox1_KeyPress
This code only allowing numbers and . and the backspace.
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if ((e.KeyChar > (char)Keys.D9 || e.KeyChar < (char)Keys.D0) && e.KeyChar != (char)Keys.Back && e.KeyChar != '.')
{
e.Handled = true;
}
//Edit: Alternative
if (!char.IsDigit(e.KeyChar) && e.KeyChar != (char)Keys.Back && e.KeyChar != '.')
{
e.Handled = true;
}
}
There are two ways you could try to approach this:
Wait for the user to finish entering the data, then use double.TryParse() to make sure it's a valid number.
Use the KeyPress event of the TextBox to validate the data as each key is pressed.
Take a look here: Simple Numeric TextBox
EDIT: As other answers explains what to do dealing with OnKeyPress/OnKeyDown events, this article demonstrates how to deal with other scenarios, as pasting text, so I'll keep this answer.
You should hook into the KeyDown and KeyPress event to prevent the input of unwanted characters. There is a sample on MSDN
Many third party control libraries also have this kind of functionality built in if you ever find yourself using one of them.
It is better practice to allow typing anything but give notification through an error provider about the failing validation (Validating event and double.TryParse()).
But if you insist, be sure to replace the '.' with the decimal separator of the system. If you are not assuming all values to be English you can easily get into a cannot-enter-a-decimal-value problem.
For instance, I am in Croatia and here we have to type a comma (,). In some islamic country I fail to remember the decimal separator is a hash (#).
So beware of localization issues.
The below code will suppress all but number, the backspace, and the decimal. It also only allows a single decimal for numeric entry.
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if ((e.KeyChar > (char)Keys.D9 || e.KeyChar < (char)Keys.D0) && e.KeyChar != (char)Keys.Back && e.KeyChar != '.')
{
e.Handled = true;
return;
}
if(e.KeyChar == '.' && textBox1.Text.Contains('.'))
{
e.Handled = true;
}
}
This also can be used
if (!Char.IsDigit(e.KeyChar) && e.KeyChar != '\b' && e.KeyChar != '.'){
e.Handled = true;}
Shortest fix to make text/cmb box read-only, attach following to respective keypress event :
private void cmbDisable_KeyPress(object sender, KeyPressEventArgs e)
{
e.KeyChar = (char)(0);
}
In the following code, the two calls to Zoom(0.1f); and Zoom(-0.1f); work but I cannot trigger the two UndoRedoManager.Undo(); and UndoRedoManager.Redo(); calls for CTRL+Z and CTRL+Y. What am I doing wrong?
public void WorkspaceKeyDown(KeyEventArgs e)
{
if (e.Control == true)
isCtrlPres = true;
if (e.Shift == true)
isShiftPres = true;
if (e.Control == true && e.KeyCode == Keys.Z)
{
UndoRedoManager.Undo();
}
else if (e.Control == true && e.KeyCode == Keys.Y)
{
UndoRedoManager.Redo();
}
else if (e.Control == true && e.KeyCode == Keys.Oemplus)
{
Zoom(0.1f);
}
else if (e.Control == true && e.KeyCode == Keys.OemMinus)
{
Zoom(-0.1f);
}
.ShortcutKeys = ((System.Windows.Forms.Keys)((System.Windows.Forms.Keys.Control | System.Windows.Forms.Keys.Z)));