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.
Related
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've a textbox with an event that should do things when some text is entered. It's easy to check if it is alphanumeric as stated here Can I determine if a KeyEventArg is an letter or number? :
if ( ( ( e.KeyCode >= Keys.A && e.KeyCode <= Keys.Z ) ||
( e.KeyCode >= Keys.D0 && e.KeyCode <= Keys.D9 ) ||
( e.KeyCode >= Keys.NumPad0 && e.KeyCode <= Keys.NumPad9 ) )
The problem with this approach is that I should also check manually for -?!¿[]() with Key.OemMinus, Key.OemQuestion, etc.
Is there some way to check if it's a text keystroke or I should check manually (which is not very elegant in my opinion)?
As no other option is suggested I used the following code to allow nearly all text keystrokes. Unfortunatelly, this is keyboard dependant so it's not very elegant. Hopefully is not a critical aspect in the application, it's only a matter of usability.
bool isText = (e.Key >= Key.A && e.Key <= Key.Z) || (e.Key >= Key.D0 && e.Key <= Key.D9) || (e.Key >= Key.NumPad0 && e.Key <= Key.NumPad9)
|| e.Key == Key.OemQuestion || e.Key == Key.OemQuotes || e.Key == Key.OemPlus || e.Key == Key.OemOpenBrackets || e.Key == Key.OemCloseBrackets || e.Key == Key.OemMinus
|| e.Key == Key.DeadCharProcessed || e.Key == Key.Oem1 || e.Key == Key.Oem7 || e.Key == Key.OemPeriod || e.Key == Key.OemComma || e.Key == Key.OemMinus
|| e.Key == Key.Add || e.Key == Key.Divide || e.Key == Key.Multiply || e.Key == Key.Subtract || e.Key == Key.Oem102 || e.Key == Key.Decimal;
this code allow just numbers and '.':
private void txtJustNumber_KeyPress(object sender, KeyPressEventArgs e)
{
if (!char.IsDigit((char)(e.KeyChar)) &&
e.KeyChar != ((char)(Keys.Enter)) &&
e.KeyChar != (char)(Keys.Delete) &&
e.KeyChar != (char)(Keys.Back))
{
e.Handled = true;
}
}
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)));
So I came across this method in code:
private void FilterBasedUponPermission(List<Data.Indications.SpWeb_SavedIndications1LightDataObject> list)
{
list.RemoveAll(item =>
(item.Permission == Controllers.Indications.ICConstants.TradeType_LLH && !isLLH) ||
(item.Permission == Controllers.Indications.ICConstants.TradeType_ALM && !isALM) ||
(item.Permission == Controllers.Indications.ICConstants.TradeType_RealEstate && !isRE) ||
(item.Permission == Controllers.Indications.ICConstants.TradeType_Auditor && !isAuditor));
}
However, in the list passed in, some of the permission values are null, and it's keeping them in the filtered list. I want it to remove the items that also don't have a permission set, not just the ones that don't match the permission that you have as a user.
Thanks!
Just add a item.Permission == null
list.RemoveAll(item => item.Permission == null ||
(item.Permission == Controllers.Indications.ICConstants.TradeType_LLH && !isLLH) ||
(item.Permission == Controllers.Indications.ICConstants.TradeType_ALM && !isALM) ||
(item.Permission == Controllers.Indications.ICConstants.TradeType_RealEstate && !isRE) ||
(item.Permission == Controllers.Indications.ICConstants.TradeType_Auditor && !isAuditor));
private void FilterBasedUponPermission(List<Data.Indications.SpWeb_SavedIndications1LightDataObject> list)
{
list.RemoveAll(item =>
(item.Permission == null) ||
(item.Permission == Controllers.Indications.ICConstants.TradeType_LLH && !isLLH) ||
(item.Permission == Controllers.Indications.ICConstants.TradeType_ALM && !isALM) ||
(item.Permission == Controllers.Indications.ICConstants.TradeType_RealEstate && !isRE) ||
(item.Permission == Controllers.Indications.ICConstants.TradeType_Auditor && !isAuditor));
}
Does on of those work?
list.RemoveAll(item => !item.Permission.HasValue); // in case Permission is Nullable<T>
list.RemoveAll(item => item.Permission == null);
You can add all the other conditions after a logical or (||).
Cheers,
Matthias