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";
}
Related
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);
}
}
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.
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)
{
}
}
I have the follwing code (which is not working):
private void Window_PreviewKeyDown(object sender, KeyEventArgs e) {
e.Handled = true;
if ((e.Key == Key.P) && (Keyboard.Modifiers == ModifierKeys.Alt)) {
MessageBox.Show("Thanks!");
}
}
Why doesn't this work? The event is firing, but
(e.Key == Key.P) && (Keyboard.Modifiers == ModifierKeys.Alt))
never evaluates to true.
My similar events using Ctrl instead of Alt in this way work. Also my events that include Ctrl and Alt work as well.
A better way to work with keys in WPF is Key Gestures
e.g. note that this is an example, not a solution
<Window.InputBindings>
<KeyBinding Command="ApplicationCommands.Open" Gesture="ALT+P" />
</Window.InputBindings>
There's more to it that that but you'll work it easily enough. That's the WPF way to handle keys!
PK :-)
You need to do a 'bitwise and' with the ModifierKeys as shown below...
private void Window_PreviewKeyDown(object sender, KeyEventArgs e)
{
if ((e.Key == Key.P) && ((e.Modifiers & ModifierKeys.Alt) == ModifierKeys.Alt))
{
MessageBox.Show("Thanks!");
e.Handled = true;
}
}
Also, do not forget to set the Handled property of the e parameter...
MSDN gives us this example:
if(e.Key == Key.P && e.Modifiers == Keys.Alt)
does this work for you?
Despite me working with C# (Windows Forms) for years, I'm having a brain fail moment, and can't for the life of me figure out how to catch a user typing Ctrl + C into a textbox.
My application is basically a terminal application, and I want Ctrl + C to send a (byte)3 to a serial port, rather than be the shortcut for Copy to Clipboard.
I've set the shortcuts enabled property to false on the textbox. Yet when the user hits Ctrl + C, the keypress event doesn't fire.
If I catch keydown, the event fires when the user presses Ctrl (that is, before they hit the C key).
It's probably something stupidly simple that I'm missing.
Go ahead and use the KeyDown event, but in that event check for both Ctrl and C, like so:
if (e.Control && e.KeyCode == Keys.C) {
//...
e.SuppressKeyPress = true;
}
Also, to prevent processing the keystroke by the underlying TextBox, set the SuppressKeyPress property to true as shown.
Key events occur in the following order:
KeyDown
KeyPress
KeyUp
The KeyPress event is not raised by noncharacter keys; however, the noncharacter keys do raise the KeyDown and KeyUp events.
Control is a noncharacter key.
You can check with this line of code:
if (e.KeyData == (Keys.Control | Keys.C))
I had a problem catching Ctrl + C on a TextBox by KeyDown. I only got Control key when both Control and C were pressed. The solution was using PreviewKeyDown:
private void OnLoad()
{
textBox.PreviewKeyDown += OnPreviewKeyDown;
textBox.KeyDown += OnKeyDown;
}
private void OnPreviewKeyDown( object sender, PreviewKeyDownEventArgs e)
{
if (e.Control)
{
e.IsInputKey = true;
}
}
private void OnKeyDown( object sender, KeyEventArgs e)
{
if (e.Control && e.KeyCode == Keys.C) {
textBox.Copy();
}
}
D'oh! Just figured it out. Out of the three possible events, the one I haven't tried is the one I needed! The KeyUp event is the important one:
private void txtConsole_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyData == (Keys.C | Keys.Control))
{
_consolePort.Write(new byte[] { 3 }, 0, 1);
e.Handled = true;
}
}
If you want to catch such combinations of keys in KeyPress Event look at this table here:
http://www.physics.udel.edu/~watson/scen103/ascii.html
in Non-Printing Characters section you can see the Dec numbers for each combination.
For example, Dec number for Ctrl + C is 3. So you can catch it in KeyPress Event like this:
private void btnTarget_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar != 3) // if it is not Ctrl + C
{
// do something
}
}
Try the following: capture the up arrow and down arrow events. When you detect down arrow for CTRL, set a flag; when you detect up arrow, reset the flag. If you detect the C key while the flag is set, you have Ctrl+C.
Edit. Ouch... Jay's answer is definitely better. :-)
I don't know if it's because some change in newer version or because I am trying to use this on ListBox, but there is no e.Control in KeyEventArgs e that I get from KeyDown.
I had to work around solution, I came up with this (it's not the prettiest one, but it works fine):
private List<Key> KeyBuff = new List<Key>();
private void ListBox_KeyDown(object sender, KeyEventArgs e)
{
if (!KeyBuff.Exists(k => k == e.Key))
KeyBuff.Add(e.Key);
if (KeyBuff.Exists(k => k == Key.LeftCtrl || k == Key.RightCtrl) &&
KeyBuff.Exists(k => k == Key.C))
{
// Desired detection
Clipboard.SetText(SelectedText);
}
}
private void ListBox_KeyUp(object sender, KeyEventArgs e)
{
KeyBuff.Clear();
}
For me, it's not working with KeyDown event so I tried with PreviewKeyDown and it's worked.
private void txt_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
if (e.Control == true && e.KeyCode == Keys.C)
{
Clipboard.SetText(txt.SelectedText);
}
}