I want to use the combination Alt+X to close the current form, but there is also one condition: if the user presses a similar combination like Alt+X,C, it shouldn't work.
The form/control has an event called key press and key down. In the properties window input 'Form1_KeyPress'. This will run the method below
void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == //KeyCode for alt && e.KeyChar == //KeyCode for x)
{
//run code here
}
}
Related
My problem
I have few buttons in a form. I need to trigger those button on mouse click or the alt + the alphabet used as the hot key in it.
I added & in front of the alphabet in the name property of the button. But my problem is that even if the alphabet is pressed without the use of alt key the below action is triggered.
The below is the method which triggers the button named FirstMatch.
public void firstMatch_Button_Click(object sender, EventArgs e)
{
Action_Raised(sender, "First Match");
}
First you need added & in front of the alphabet in the Text property not Name property of the button.
What you want to do is achieve by following code.
private void button1_Click(object sender, EventArgs e)
{
if (ModifierKeys.HasFlag(Keys.Alt) || e.GetType() == typeof(MouseEventArgs))
{
MessageBox.Show("button is clicked.");
}
}
You could change the property of your form KeyPreview to True
Now the form receives every key event first.
Then you can add an key up/down event to your form like this:
private void Form1_KeyUp(object sender, KeyEventArgs e)
{
// Replace "Keys.A" with the key you want
if (e.KeyCode == Keys.A && e.Alt)
{
this.firstMatch_Button.PerformClick();
}
}
And please remove the & otherwise the button gets further triggered.
I am doing my graduation work and I need a little help.
So far I've done most of my work and now I need someone to help me with the shortcuts.
This is how my work looks like (for now):
I need to make shortcuts for those buttons so that user doesn't have to click on button with mouse. I wish to make it possible that when user presses F1 on the keybord - it's like he pressed button1 with mouse. F2 stands for button2 and so on.
Shortcuts as CTRL+ some key are also acceptable. I only need example how to make couple buttons, and i will make others :)
Thanks
Set Form1's KeyPreview-property to true in the designer or in the code:
this.KeyPreview = true;
Then add KeyUp-action to your form, which accepts all keys (even Delete etc):
private void Form1_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Delete)
{
button1ClickMethod();
}
}
Then separate your buttons' actions to individual methods, like button1ClickMethod(), and avoid using the Button1_Click(null, null);. Call this button1ClickMethod() in the KeyUp-event when a desired key is up.
To use combinations, you can use this:
// If CTRL and F1 were pressed
if (e.Control && e.KeyCode == Keys.F1)
{
MessageBox.Show("Shortcut CTRL + F1 was pressed!");
}
You can also check if the Shift was pressed at the same time with e.Shift-property, same way like the e.Control.
KeyPress event will help you
Sample code ;
private void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
//Call the event according to key
// if(e.KeyChar == "")
//btnSave_Click(null,null)
}
private void timer1_Tick(object sender, EventArgs e){
if (Form.ModifierKeys == System.Windows.Forms.Keys.Control
&& Form.ModifierKeys == System.Windows.Forms.Keys.Enter)
my_translate(textbox1.text);
}
.
I try it but dont work how can I do it?
I am writing a dictionary software; with timer I check determine pressed keys so I translate word. I cant use textBox1_KeyPress etc. because I get text from .doc/.txt so I need timer for get text.
//The code is working
private void timer1_Tick(object sender, EventArgs e){
MouseButtons aa = MouseButtons;
if (aa == MouseButtons.Middle && Form.ModifierKeys == Keys.Control)
my_translate();
}
.
And We have a alnernative for timer to call a method when user pressed a key combination?
Your current code
if (Form.ModifierKeys == System.Windows.Forms.Keys.Control &&
Form.ModifierKeys == System.Windows.Forms.Keys.Enter)
means "if the keys pressed equals the control key AND the keys pressed equals the enter key". This will never happen, because if only the control key is pressed, the enter key is not pressed, and vise versa.
I believe you wanted this:
if (Form.ModifierKeys.HasFlag(Keys.Control) &&
Form.ModifierKeys.HasFlag(Keys.Enter))
This means "if the keys pressed includes the control key and the enter key".
You shouldn't use timers for this anyways. Look into the Control.KeyPress event and use that instead. You can use the timer to load the text file while using events to handle the key press.
I suggest you read more about the KeyPress event on MSDN: http://msdn.microsoft.com/en-us/library/system.windows.forms.control.keypress.aspx
Hello I am trying to match the button down visual (on the WinForm, the button boarder gets a little darker, indicating it is pressed) with a keydown event. First of all I need to detect a keydown for numbers only. Then when the key goes down, the corresponding number button on the form should look like it is depressed as well. Sorry if this is already been answered using differt jargon. I already know how to perform a button click with the keydown.
Make a test code on KeyDown event. Write down the keyboard codes you shall see from pressing 0 to 9. Then use those keyboard codes in your KeyDown's if statement
You can use a Checkbox and set the appearance to be Button. Then you can do something like this:
private void OnKeyDown(object sender, KeyEventArgs e)
{
//if key
checkBox1.Checked = true;
}
private void OnKeyUp(object sender, KeyEventArgs e)
{
//if key
checkBox1.Checked = false;
}
As far as the Keys, you can just use the KeyEventArgs.KeyCode
e.KeyCode == Keys.D0 || .. || e.KeyCode == Keys.D9
I have a listbox which displays the contents of an array. The array is populated with a list of results when my "go" button is pressed.
The go button is set as the AcceptButton on the form properties so pressing the Enter key anywhere in the focus of the form re-runs the go button process.
Double clicking on a result from the array within the listbox works fine using below:
void ListBox1_DoubleClick(object sender, EventArgs e) {}
I would like to be able to use my arrow keys and enter keys to select and run an event without having to double click on the line within the listbox. (however go button runs each time instead)
Basically open the form, type search string, press enter to run go button, use up and down arrows then press enter on selection to run same event as double click above. Will need to change focus after each bit.
You can handle the KeyDown events for the controls you want to override. For example,
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
//execute go button method
GoButtonMethod();
//or if it's an event handler (should be a method)
GoButton_Click(null,null);
}
}
That will perform the search. You can then focus your listbox
myListBox.Focus();
//you might need to select one value to allow arrow keys
myListBox.SelectedIndex = 0;
You can handle the Enter button in the ListBox the same way as the TextBox above and call the DoubleClick event.
This problem is similar to -
Pressing Enter Key will Add the Selected Item From ListBox to RichTextBox
Certain controls do not recognize some keys when they are pressed in Control::KeyDown event. For e.g. list box does not recognize if the key pressed is Enter key.
See the remarks section of the Control::KeyDown event reference.
One way to resolve your problem might be writing a method for the Control::PreviewKeyDown event for your list box control:
private void listBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Up && this.listBox1.SelectedIndex - 1 > -1)
{
//listBox1.SelectedIndex--;
}
if (e.KeyCode == Keys.Down && this.listBox1.SelectedIndex + 1 < this.listBox1.Items.Count)
{
//listBox1.SelectedIndex++;
}
if (e.KeyCode == Keys.Enter)
{
//Do your task here :)
}
}
private void listBox1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
switch (e.KeyCode)
{
case Keys.Enter:
e.IsInputKey = true;
break;
}
}