How to get key code for shift+tab in windows application? - c#

Hi have an windows application that should focus to previous text box with change in back color when shift+tab is pressed.

e.Modifiers == Keys.Shift && e.KeyCode == Keys.Tab
in an event handler method, using an if condition maybe.

Try this,
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Tab && e.Shift)
{
}
}

Related

Ctrl+C on ListView recognises as "LButton | Shift Key"

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";
}

How to perform action when Shift+Enter is pressed?

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);
}
}

C# - How to override actions for "Up arrow" and "Down arrow" for a textbox?

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...
}
}

Prevent images in rich text box

I am working on a C# winForms application where I am using lots of RichTextBoxes. I found out that if I copied an image and pasted that in any RichTextBox, the image would be posted. Is there a way not to allow images to be pasted in the RichTextBox. In other words, to only allow keyboard characters.
The problem with the answer above is that it doesn't work in cases where there is mixed content. For example if you highlight a few rows from a spreadsheet and paste into a richtextbox you end up with more than just the raw text. I think the better solution is below:
private void richTextBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.Control && e.KeyCode == Keys.V)
{
if (Clipboard.GetData("Text") != null)
Clipboard.SetText((string)Clipboard.GetData("Text"), TextDataFormat.Text);
else
e.Handled = true;
}
}
EDIT: The method below was shared by MrCC and is a more direct / better approach than my method above.
private void richTextBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.Control && e.KeyCode == Keys.V)
{
if (Clipboard.ContainsText())
richTextBox1.Paste(DataFormats.GetFormat(DataFormats.Text));
e.Handled = true;
}
}
I was able to answer my question. Here it is in case someone else was looking for it.
private void InputExpressionRchTxt_KeyDown(object sender, KeyEventArgs e)
{
bool ctrlV = e.Modifiers == Keys.Control && e.KeyCode == Keys.V;
bool shiftIns = e.Modifiers == Keys.Shift && e.KeyCode == Keys.Insert;
if (ctrlV || shiftIns)
if (Clipboard.ContainsImage())
e.Handled = true;
}
Maybe, you can catch paste event and check what object copied to RichTextBox.
If it Image, just delete it.

Capture combination key event in a Windows Forms application

When the user presses the Shift + UP keys, I want my form to respond by calling up a message box.
How do I do this in Windows Forms?
Handle the KeyDown event and have something like:
if (e.Modifiers == Keys.Shift && e.KeyCode == Keys.Up)
{
MessageBox.Show("My message");
}
The event handler has to be on the Main Form and you need to set the KeyPreview property to true. This can be done in design mode from the properties dialog.
In case you want to use multiple modifiers KeyEventArgs also has boolean values to indicate if CTRL, ALT or SHIFT is pressed.
Example:
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.Control && e.Alt && e.Shift && e.KeyCode == Keys.F12)
MessageBox.Show("My message");
}
In this example the messagebox is show if CTRL, ALT, SHIFT and F12 are pressed at the same time.
To handle multiple modifiers keys ( In KeyDown event)
if (e.Control && e.Shift)
{
if (e.KeyCode == Keys.F1)
{
// Your code goes here
}
}

Categories