Using KeyDown event for single action - c#

I am trying to use the KeyDown-Event (because I like to use KeyCode) to make a single action happen. For this purpose I am using a bool variable to stop continuous actions.
Can't figure out what's wrong with my code though, and haven't found a comparable problem/solution yet...
There are 2 tabs on my tabcontrol and i want to be able to switch between them using CTRL+TAB.
The switching should happen ONCE on keydown of tab.
bool tabSwitchPossible = true;
void Form1_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Tab)
tabSwitchPossible = true; //Reset boolean
}
void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (tabSwitchPossible && e.KeyCode == Keys.Tab && e.Modifiers == Keys.Control)
{
tabSwitchPossible = false; //Set boolean to prevent further action
if (mainTabControl.SelectedIndex >= mainTabControl.TabCount - 1)
mainTabControl.SelectedIndex = 0;
else
mainTabControl.SelectedIndex++;
return;
}
}
Is there an automatic KeyUp event fired, even when i don't release the key?!
Thanks, in advance guys...

You don't need to handle Form1_KeyUp and tabSwitchPossible varible, remove it and just copy the following code:
void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Tab && e.Modifiers == Keys.Control)
{
if (mainTabControl.SelectedIndex >= mainTabControl.TabCount - 1)
mainTabControl.SelectedIndex = 0;
else
mainTabControl.SelectedIndex++;
}
}

Related

How to overwrite ctrl + a select all from list box?

I was hoping that this code below would overwrite it, since I am assigning new stuff. But instead it executes both, selecting all and my message box
private void EventSetter_OnHandler(object sender, KeyEventArgs e)
{
if (e.Key == Key.A && (Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl)))
{
System.Windows.MessageBox.Show("ctrl a");
}
}
please help thanks
If you handle the PreviewKeyDown event for the ListBox, you should be able to mark the event as handled, and the Ctrl+A should be ignored:
private void OnListBoxKeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.A && (Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl)))
{
e.Handled = true;
}
}

C# - How can I block the typing of a letter on a key press?

I have a textbox with a OnKeyPress event. In this textbox I wish to input only numbers, and for some specific letters like t or m, I would want to execute a code without that letter being typed in the textbox. Small sample of what I am trying to do:
//OnKeyPressed:
void TextBox1KeyDown(object sender, KeyEventArgs e)
{
if(e.KeyCode == Keys.T || e.KeyCode == Keys.M) Button1Click(this, EventArgs.Empty);
}
This unfortunately does not prevent the input of the letter..
Set the SuppressKeyPress property from KeyEventArgs to true, like below:
private void TextBox1KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.T || e.KeyCode == Keys.M)
{
e.SuppressKeyPress = true;
Button1Click(this, EventArgs.Empty);
}
}
You could always run the TryParse on the keyDown event so as to validate as the data gets entered. It saves the user an additional UI interaction.
private void TextBox1KeyDown(object sender, KeyEventArgs e)
{
int i;
string s = string.Empty;
s += (char)e.KeyValue;
if (!(int.TryParse(s, out i)))
{
e.SuppressKeyPress = true;
}
else if(e.KeyCode == Keys.T || e.KeyCode == Keys.M)
{
e.SuppressKeyPress = true;
Button1Click(this, EventArgs.Empty);
}
}

Save a text string to textbox with keyboard shortcut

Im a beginner in C# and im trying to create a windows form application that save a copied text to a textbox when you execute a command with the keyboard. I know there is mouch more to do but where do i start? I suceed to make someting happening with the code below a start at least..
And another question.. is it possible to create more than 2 commands. It doesn't work if i add for example : " && KeyCode.ToString() == "B") "
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.Control && e.KeyCode.ToString() == "C")
{
MessageBox.Show("CTRL+C");
}
}
Cheers
You would manage this by calling keyDown/Up events. Keep track of each event and which key went down. Then using the Clipboard.GetText() function to copy/paste the text from clipboard into your textbox once both keys are down.
Example,
bool keyup = false;
bool keyleft = false;
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Up)
{
keyup = true;
}
else if (e.KeyCode == Keys.Left)
{
keyleft = true;
}
if (keyleft && keyup)
{
textboxOne.Text = Clipboard.GetText(TextDataFormat.Html);
}
}
private void Form1_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Up)
{
keyup = false;
}
else if (e.KeyCode == Keys.Left)
{
keyleft = false;
}
}
Used both these as my resources.
Resource One: Detect when two keys are pressed at the same time
Resource Two: http://msdn.microsoft.com/en-us/library/c2thcsx4%28v=vs.110%29.aspx

how to detect two hot key in win form app like CTRL +C , CTRL+ W

my question is how to detect two hot keys in win form application
CTRL +C,CTRL,K like visual studio commenting command
I need to simulate VS hot key For commenting a line of code
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.Modifiers == Keys.Control && e.KeyCode == Keys.C)
{
}
}
Simple way is... There is a Windows API Function called ProcessCmdKey, by overriding this function we can achieve what we want
protected override bool ProcessCmdKey(ref Message msg, Keys keyData) {
if (keyData == (Keys.Control | Keys.C)) {
MessageBox.Show("You have pressed the shortcut Ctrl+C");
return true;
}
return base.ProcessCmdKey(ref msg, keyData);
}
Microsoft Documentation can be found here
source
private bool _isFirstKeyPressedW = false;
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.Control & e.KeyCode == Keys.W)
{
_isFirstKeyPressedW = true;
}
if (_isFirstKeyPressedW)
{
if (e.Control & e.KeyCode == Keys.S)
{
//write your code
}
else
{
_isFirstKeyPressedW = e.KeyCode == Keys.W;
}
}
}
if you want to handle Ctrl + C followed by Ctrl+ K you need to maintain a state variable.
Set the Form KeyPreview property to true and handle the Form KeyDown event.
Try This:
this.KeyPreview=true;
private bool isFirstKeyPressed= false;
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.Control && e.KeyCode == Keys.C)
{
isFirstKeyPressed = true;
}
if (isFirstKeyPressed)
{
if (e.Control && e.KeyCode == Keys.K)
{
MessageBox.Show("Ctrl+C and Ctrl+K pressed Sequentially");
/*write your code here*/
isFirstKeyPressed= false;
}
}
}

How to know when ctrl+space pushed?

I want to call function when Ctrl+space pushed. I searched more but couldn't find what I want.
You need to add an event handler for KeyDown like: KeyDown="TextBox_KeyDown" on your TextBox.
And then in the event handler:
if (e.Key == Key.Space && e.KeyboardDevice.Modifiers == ModifierKeys.Control)
{
//Do Stuff
}
Use something like this:
private void Window_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Space &&
(Keyboard.Modifiers & ModifierKeys.Control) == ModifierKeys.Control)
{
// Do what you need here
}
}
This should get you working -
private void Window_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Space && Keyboard.Modifiers == ModifierKeys.Control)
{
}
}
If you want to catch all the keys, whether you have the focus or not, in your class you just need to add in the constructor:
// To capture keyboard
EventManager.RegisterClassHandler(typeof(Window), Keyboard.KeyDownEvent, new System.Windows.Input.KeyEventHandler(keyDown), true);
And add the method: (it's an example, it's not for adapted for what you want)
private void keyDown(object sender, System.Windows.Input.KeyEventArgs e)
{
if (e.Key == Key.Space)
{
code;
}
else if ((Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl)) && Keyboard.IsKeyDown(Key.T))
{
code;
}
}

Categories