Disable all shortcuts in a text box - winforms

I am aware of the shortcuts enabled property, however, It does not disable all shortcuts.
For example, zooming with ctrl+mousewheel or writing superscript letters with ctrl+shift+plus.
If you're giving me a solution that disables shortcuts by their name, id, keys, etc., please give me a list of all the shortcuts and their names, ids, keys etc.

Eureka!
To handle a key press by myself without letting the form handle it simply:
void OnRichTextBoxKeyDown(object sender, KeyEventArgs e)
{
if(e.Control && e.Shift && e.KeyCode == Keys.Oemplus)
{
MessageBox.Show("Why Would I ever Want to Write Superscript Letters?!");
e.Handled = true;
}
}
Now here is a list of all the shortcuts coming with a rich text box, although, I tried some of them out and they didn't work, so you got to check first.

Related

Combobox User manual input and Selection change

I am trying to raise two different action which will expose additional field when ever there is a selection change on the Combobox and Manual Text input.
However, one triggers the other.
SelectionChanged="ComboBoxAccount_SelectionChanged"
TextBoxBase.TextChanged="ComboBoxAccount_textChanged"
How could I exactly determine there was a user manual input and not selection change which being population by different selections on the form and by doing that raise two different events?
I have used on the ComboBoxAccount_SelectionChanged() the comboxAccount.IsKeyboardFocusWithin check to determine if it is manual input or not.
private void ComboBoxAccount_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (this.comboxAccount.IsDropDownOpen && comboxAccount.comboxAccount.IsKeyboardFocusWithin)
{.....}
else if (comboxAccount.IsKeyboardFocusWithin)
{....}
}

How to detect if hash key (#) is pressed in WPF?

I'm trying this code in my PreviewKeyDown event to detect if hash key (#) is pressed or not but it still show number (3) in my TextBox and MessageBox:-
private void text_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.D3 && Keyboard.Modifiers == ModifierKeys.Shift)
MessageBox.Show(e.Key.ToString());
}
How can I modify or change this code to do what I need ?!
Thanks.
KeyDown captures the last key that was pressed down, which was Key.D3, hence why you see 3 in your output. This is expected behavior.
Why would you want to use e.Key.ToString() though? This would make sense if you wanted to see which key is being pressed, but you are explicitly checking for the SHIFT + 3 combo beforehand, so you already know what you're after.
// slightly modified your check
if (e.Key == Key.D3 && (Keyboard.Modifiers & ModifierKeys.Shift) == ModifierKeys.Shift)
{
MessageBox.Show("SHIFT+3 combo pressed");
}
Also, as #keyboardP noted in the comments, the location of the # sign on the keyboard depends on the layout of the keyboard itself (US/UK etc), or as you have it configured by your operating system. Perhaps you could add checking of the keyboard layout into the mix (might be worth having a look at the InputLanguageManager class from the System.Windows.Input namespace).
EDIT: As an alternative, why not just track whether the # character was input in the textbox, rather than checking for keyboard combos? It seems much more straightforward to me, though it depends on your requirements.

Overrite shift behavior in NumericUpDown

Per user request I implemented NumericUpDown with varying Increment (0.1 on arrow press, 0.01 on SHIFT+arrow press) as suggested in this answer. It works very well when the user interacts with the mouse to press the control's arrows.
However, the keyboard input with the Up arrow does not trigger UpButton() when the SHIFT is pressed (same for the Down key and the DownButton()). Instead, the text in the control is being selected. Since the normal behavior is for both methods to be triggered when the keyboard arrows are pressed, of course I would like to keep this functionality on for the SHIFT combination as well.
So far I have tried overriding every method in the NumericUpDown which contains the "Key" word but none could help me. ProcessCmdKey can detect the key combination and disable the default selection behavior but it doesn't not lead to UpButton/DownButton being triggered either.
Any ideas?
Shift + Up/Down are text selection shortcut keystrokes for the textbox portion of the NumericUpdate. Just as they are for a regular TextBox. Disabling this feature isn't the greatest idea.
Multiple ways to do this, perhaps the easiest is to detect the keystroke in an override for the form's ProcessCmdKey() method. So they'll work on any NUD and the code changes are small. I'll post the C# version of it, creating a winforms app in C++/CLI is too much of a hassle these days:
protected override bool ProcessCmdKey(ref Message msg, Keys keyData) {
var ctl = this.ActiveControl as NumericUpDown;
if (ctl != null) {
if (keyData == (Keys.Up | Keys.Shift)) {
ctl.Value = Math.Min(ctl.Maximum, ctl.Value + 10 * ctl.Increment);
return true;
}
if (keyData == (Keys.Down | Keys.Shift)) {
ctl.Value = Math.Max(ctl.Minimum, ctl.Value - 10 * ctl.Increment);
return true;
}
}
return base.ProcessCmdKey(ref msg, keyData);
}

detecting if shift was held when application started

Is there anyway that I can find out if user has been holding Shift (CTRL or any other key) when she/he double click on application icon from desktop to start the application?
I have a WPF application where I want to be able to detect if the user has been holding any special key when he/she started the application (by double clicking) so I can change some settings if the key was pressed.
I tried :
private void Application_Startup(object sender, StartupEventArgs e)
{
}
but couldn't find any way to detect the key down .
Write this code in your application start-up event:
// Instead of the MessageBox you could write your code here
if ((Keyboard.Modifiers & ModifierKeys.Shift) > 0)
{
MessageBox.Show("Shift Pressed");
}
I think you should look at this question: Keyboard modifiers during application startup This is probably what you're looking for.
Hope it helps.

How do I capture keystrokes in a Silverlight application?

I am trying to capture any keystroke that happens inside my Silverlight 2 application. There are not any input fields, I'm writing a game, and need to know which of the arrow keys are being pressed. My best guesses at how to capture these through an event handler are unsuccessful. Any recommendations?
Good to see you again (virtually).
I'm assuming you already tried wiring KeyDown on the root UserControl element. Here are some tips:
The plugin needs focused before it sees key events. You'll have to force the user into clicking on the plugin to start.
Make sure you don't have another element (like the ScrollViewer) that is eating arrow keys events. If you have a ScrollViewer in play you'll only be seeing KeyUp.
No switching to full screen mode.
Must be something simple you are missing like that. Hope that helps.
Handle the KeyDown and/or the KeyUp event on your root Grid.
Silverlight 2 supports the KeyUp and KeyDown events (only, I believe - not KeyPress).
In these events, you get a KeyEventArgs object as a parameter. You can get the KeyCode or KeyData from this.
In reviewing a few different threads from different forums regarding this matter it would seem it is best to handle your keyevents on your Page (root) element, parse the key for the desired effect, and redirect your command to the particular control.
Page
this.KeyDown += (s, e) =>
{
MyControl control = (MyControl)Layoutroot.FindName("controlname");
if(control != null)
{
control.MyPublicFunction(e.Key);
}
};
MyControl
public MyPublicFunciton(Key pressedKey)
{
if(pressedKey == Key.Enter)
{
//Do something
}
}

Resources