I have a TextBox in a C#/XAML desktop app and I want to detect the Shift+Enter command. How can I do this?
So far I have only been able to find information on commands like Ctrl+A, etc.
ModifierKeys.Shift allows you to identify key pressed combinations which includes Shift:
private void HandleKeyDownEvent(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter && (Keyboard.Modifiers == ModifierKeys.Shift))
{
// Handle..
}
}
Another option is Keyboard.IsKeyDown static method (see Shoe's answer).
if (Keyboard.Modifiers == ModifierKeys.Shift && Keyboard.IsKeyDown(Key.Enter))
{
MessageBox.Show("test");
}
A good example can be found here.
public void TextBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if ((Control.ModifierKeys & Keys.Shift) == Keys.Shift)
{
MessageBox.Show("Pressed " + Keys.Shift);
}
}
Related
I'm creating an UWP app (C# .NET) where there is textbox. I want to implement a shortcut (Ctrl+F) to search texts in the textbox. I know how to find texts, but I don't know how to implement the shortcut.
I found this:
if ((e.Control && e.KeyCode == Keys.F) || (e.Control && e.KeyCode == Keys.S))
{
//do something
}
...but it isn't working for UWP. I tried this (textarea is name of textbox):
private void textarea_KeyDown(object sender, KeyRoutedEventArgs e)
{
if ((e.Key == Windows.System.VirtualKey.Control) && (e.Key == Windows.System.VirtualKey.F))
{
flayoutFind.ShowAt(appBarButtonFind as FrameworkElement);
}
}
but it isn't working too. How can I do it?
And for the future, is there any way, how to override default functionality and shortcut of textbox Ctrl+Z (undo)?
You should be using "Accelerators" and "Access keys" as described here:
https://learn.microsoft.com/en-us/windows/uwp/input-and-devices/keyboard-interactions
Basically, you will have to register for events
Window.Current.CoreWindow.Dispatcher.AcceleratorKeyActivated += Dispatcher_AcceleratorKeyActivated;
private void Dispatcher_AcceleratorKeyActivated(CoreDispatcher sender, AcceleratorKeyEventArgs args)
{
//Implementation
}
You can check the sample in detail here: https://github.com/Microsoft/DesktopBridgeToUWP-Samples/blob/master/Samples/SQLServer/BuildDemo/MainPage.xaml.cs
How can I catch "ctrl+c" keys pressed on listview?
I'm trying like that
private void listviewLogger_KeyUp(object sender, KeyEventArgs e)
{
if (sender != listviewLogger) return;
//if (e.Control && e.KeyData == (Keys.Control | Keys.C))
if (e.Control && e.KeyCode == Keys.C)
CopySelectedValuesToClipboard();
}
but it shows me the combination of LButton | Sift Key when I press ctrl+C:
P.S.: have two languages installed in windows, system Win2012 R2
Update1: thank You for comment! If I log actions, I see this:
e.KeyData: ControlKey
e.KeyCode: ControlKey
e.KeyData: C
e.KeyCode: C
But still cannot catch this key sequence. Code:
private void listviewLogger_KeyUp(object sender, KeyEventArgs e)
{
if (sender != listviewLogger)
return;
Logger("e.KeyData: " + e.KeyData);
Logger("e.KeyCode: " + e.KeyCode);
}
Update2:
Resolved like this. Don't ask my how :-D
if (((e.KeyData & Keys.ControlKey) != Keys.ControlKey) && e.KeyCode == Keys.C)
CopyLogEntriesToClipboard();
Update3:
Previous works for KeyUp event. For KeyDown first code-snippet works
It is better to catch key down event (I've checked it on editor by holding Ctrl+C and switching to another up without relesing buttons).
Please try one more time your first construction. It is works for me!
private void listView1_KeyDown(object sender, KeyEventArgs e)
{
if (e.Control && e.KeyCode == Keys.C)
Text = "got it";
}
I need to be able to detect if an "undo" has been triggered, and whether or not it has had an effect on the contents of my RichTextBox.
Of I type content into the RichTextBox at the minute, and press Ctrl+Z, windows seems to handle the undo for me. I want to be able to write code that will get triggered straight after that. I have been looking around and can't find anything.
Thanks in advance.
Starting from .Net 3.0, there is a simple built-in way to get notified when an undo command (among others) is executed:
CommandManager.RegisterClassCommandBinding(typeof(MyClass),
new CommandBinding(ApplicationCommands.Undo, OnUndo));
Just call this line of code in the static constructor (or somewhere else) and add a static method:
private static void OnUndo(object sender, ExecutedRoutedEventArgs e)
{
//your code
}
WINFORM:
You could exploit the KeyDown event and detect if Ctrl+Z is pressed:
richTextBox.KeyDown += new KeyEventHandler(richTextBox_KeyDown);
private void richTextBox_KeyDown(object sender, KeyEventArgs e){
if (e.Modifiers == Keys.Control && e.KeyCode == Keys.Z){
//undo detected, do something
}
}
WPF :
richTextBox.KeyUp += new KeyEventHandler(richTextBox_KeyUp);
void richTextBox_KeyUp(object sender, KeyEventArgs e) {
if (Keyboard.Modifiers == ModifierKeys.Control && e.Key == Key.Z) {
//undo detected, do something
}
}
I think you're going to have to implement that yourself. I'm not aware of an event out of the box that will suit your needs.
You might also want to have a look at Monitored Undo Framework.
And here for additional reading.
If I well understand you, you want to compare content before and after the Ctr+Z.
Then you should do :
In XAML File :
<RichTextBox PreviewKeyDown="RichTextBox_PreviewKeyDown" KeyUp="RichTextBox_KeyUp" />
In CS File :
private void RichTextBox_KeyUp(object sender, KeyEventArgs e)
{
if (Keyboard.Modifiers == ModifierKeys.Control && e.Key == Key.Z)
{
Console.WriteLine("After : " + new TextRange(((RichTextBox)sender).Document.ContentStart, ((RichTextBox)sender).Document.ContentEnd).Text);
}
}
private void RichTextBox_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (Keyboard.Modifiers == ModifierKeys.Control && e.Key == Key.Z)
{
Console.WriteLine("Before : " + new TextRange(((RichTextBox)sender).Document.ContentStart, ((RichTextBox)sender).Document.ContentEnd).Text);
}
}
Then, you will see in the output of your application the content of your RichTextBox before the Ctrl+Z and the content after.
I've try it and it works fine !
As already described, CommandBindings can be used. I prefer binding to each control instead of binding to all controls of a specific class. This can be done in the following way:
this.richTextBox.CommandBindings.Add(
new CommandBinding(ApplicationCommands.Undo, this.RichTextBoxUndoEvent));
private void RichTextBoxUndoEvent(object sender, ExecutedRoutedEventArgs e)
{
e.Handled = true;
this.richTextBox.Undo();
}
I have a textbox and below it i have a listbox.
While the user is typing in the textbox if he presses the up or down arrow he should make a selection in the listbox. The textbox detects all the characters (except space) but it seems that it can't detect the arrow presses.
Any solution for this? This is a WPF project btw.
EDIT, Here's the working code thanks to T.Kiley:
private void searchBox_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (e.IsDown && e.Key == Key.Down)
{
e.Handled = true;
//do your action here
}
if (e.IsDown && e.Key == Key.Up)
{
e.Handled = true;
//do another action here
}
}
I just tried this and it works. Add a preview key down event to the textbox
private void TextBox_PreviewKeyDown(object sender, System.Windows.Input.KeyEventArgs e)
{
if (e.IsDown && e.Key == Key.Down)
MessageBox.Show("It works");
}
You can listen to they KeyDown event of the TextBox. In the handler, check whether the arrow key was pressed (you might need to listen to key up to avoid triggering your code multiple times if the user holds down the button for too long).
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Down)
{
// Do some code...
}
}
I have a from that has a text box and I'm trying to determine if Ctrl-R is pressed within this text box. I can detect the keys separately using:
private void CheckKeys(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
if(e.KeyChar == (char)Keys.R)
{
// ...
}
if ((Control.ModifierKeys & Keys.Control) == Keys.Control)
{
// ...
}
}
How do I determine if they pressed at the same time?
If possible, change your event to KeyDown/KeyUp, everything will be easier. (Note that this solution is not always applicable)
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyData == (Keys.Control | Keys.R))
{
}
}
See Mitch's answer on how to construct the bit flag logic correctly, as long as he undeletes it. Here's something that will work, if he doesn't decide to. You basically need to check if both conditions are true at the same time:
bool isRKeyPressed = e.KeyChar == (char)Keys.R;
bool isControlKeyPressed = (Control.ModifierKeys & Keys.Control) == Keys.Control;
if (isRKeyPressed && isControlKeyPressed)
{
// Both ...
}
else if (isRKeyPressed)
{
// R key only ...
}
else if (isControlKeyPressed)
{
// CTRL key only ...
}
else
{
// None of these...
}
Throw away any of these checks that you don't care about.
Also, you might want out check out this alternative approach:
http://www.codeguru.com/columns/experts/article.php/c4639
They override the ProcessCmdKey method on their form (possibly on individual controls?):
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.processcmdkey.aspx