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
Related
I have registed ctrl+` as a hotkey,and I want to display a window when I press the hotkey and not release the leftctrl key and toggle ` key to do something else just like alt+tab switch the application.Here is the code.
private void ListOnKeyDown(object sender, KeyEventArgs e)
{
if (e.SystemKey==Key.LeftCtrl && e.Key == Key.Oem3)
{
m_host.SelectNext();
}
}
But I found this way only fired one key.So what's the right way to fire the key event?
And here is the debug information.
debug information
Just change your keydown event to detect the modifier this way
private void ListOnKeyDown(object sender, KeyEventArgs e)
{
if (Keyboard.Modifiers == ModifierKeys.Control && e.Key == Key.Oem3)
{
m_host.SelectNext();
e.Handled = true;
}
}
Because onKeyDown event works for ONE key only, in order to use two keys at same time you need little improvise:
bool firstkeyisOn = false;
private void ListOnKeyDown(object sender, KeyEventArgs e)
{
if (e.SystemKey==Key.LeftCtrl/*Or other key by choice*/)
{
firstkeyisOn = true;
e.Handled = true;
return;
}
if(firstkeyisOn && (e.Key == Key.Oem3/*Or other key by choice*/))
{
m_host.SelectNext();
}
}
private void ListOnKeyUp(object sender, KeyEventArgs e)
{
if (e.SystemKey==Key.LeftCtrl/*Key must be same as holding one*/)
{
firstkeyisOn = false;
}
//or
//firstkeyisOn = false;
}
To cancel hotkey mode you just add firstkeyisOn = false under OnKeyUp event and you good to go.
Hello I just started in Xamarin with an Android project and I encountered a problem on the keypress of an edittext. When the Enter is fired on my first Edittext and it gives focus to the Edittext (txtArtikel), but then the keypress of this Edittext is triggered and jumps to the third Edittext. So it skips my second Edittext with just one Enter press. Can anyone help me?
txtArtikel.KeyPress += txtArtikelPress;
private void txtArtikelPress(object sender, View.KeyEventArgs e)
{
e.Handled = false;
if (e.KeyCode == Keycode.Enter)
{
txtAantal.RequestFocus();
}
}
EDIT
I'm still working with the Keypress and now looking for an Enter and Escape. When the txtWerf is empty it jumps to txtArtikel, but when it contains text it goes to txtAantal.
private void txtWerfPress(object sender, View.KeyEventArgs e)
{
if (e.KeyCode == Keycode.Enter && e.Event.Action == KeyEventActions.Down)
{
if (txtWerf.Text.Trim() != "")
{
if (txtArtikel.RequestFocus())
e.Handled = true;
}
}
else
e.Handled = false;
}
private void txtArtikelPress(object sender, View.KeyEventArgs e)
{
if (e.KeyCode == Keycode.Enter && e.Event.Action == KeyEventActions.Down)
{
if (txtArtikel.Text.Trim() != "")
{
if (txtAantal.RequestFocus())
e.Handled = true;
}
}
else if (e.KeyCode == Keycode.Escape && e.Event.Action == KeyEventActions.Down)
{
if (txtWerf.RequestFocus())
e.Handled = true;
}
else
Scanner.CheckForScannedData(sender, ref e);
e.Handled = false;
}
private void txtAantalPress(object sender, View.KeyEventArgs e)
{
if (e.KeyCode == Keycode.Escape && e.Event.Action == KeyEventActions.Down)
{
if (txtArtikel.RequestFocus())
e.Handled = true;
}
else
e.Handled = false;
}
You don't need to handle the KeyPress event in order to set the focus to the next EditText.
Android has a good algorithm that handles the focus when editing is done.
How does the app behave on pressing enter when you remove the event handler? Does it set the focus to the correct EditText? If it focuses he wrong view then you should take a look on how to
Move to another EditText when Soft Keyboard Next is clicked on Android.
If the focus doesn't change at all then you should tell Android what to do when the user has completed his input by using android:imeOptions: http://developer.android.com/guide/topics/ui/controls/text.html#Actions.
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);
}
}
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;
}
}
}
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++;
}
}