Detect user's mouse click and enter key press - c#

I have a little problem that needs your ideas, I am building a simple application to detect when user finishes writing text into a textbox, there is a button the user can click or press the enter key to proceed further action
[UPDATE]
Oh I am sorry, I have to add that the textbox is from another windows, it may be any windows.
I don't want to use function like GetAsyncKeyState which is an ugly method.

Use Control.KeyPress Event
this.textBox1.KeyPress += new System.Windows.Forms.KeyPressEventHandler(CheckKeys);
private void CheckKeys(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
if (e.KeyChar == (char)13)
{
// Then Enter key was pressed
}
}
(char)13 means the Enter key.

Related

Programmatically change AcceptButton property and trigger ButtonClickEvent inside a KeyPress event

I have a page with two independent forms; users would only ever fill out and submit one or the other. When the user presses enter, I would like to trigger the click event of the button of the form that currently has focus. How can I accomplish this?
Trigger Button click using .PerformClick() method on KeyPress event of the TextBox after verifying whether pressed key is Enter
private void textBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
// Check if pressed key is `Enter` key
if (e.KeyChar == (char)Keys.Return){
button1.PerformClick();
}
}

How to accept Ctrl+Enter as return while processing Enter event on a Textbox in WPF?

I'm making a chat window and I want to send the message when the user presses the Enter key on the TextBox.
But I also want to let the user enter line breaks using Ctrl+Enter key.
The problem is when I set AcceptsReturn=True and the user presses the Enter key the KeyDown event fires after the TextBox appends a line break, so the sent message would always contain a line break at the end. Is there a way to disable the Enter key while still allowing Ctrl+Enter?
I came up with the ugliest way, which is when the Enter key is pressed first remove the letter before the cursor and then process it. But is there a better way?
I'm not 100% sure of your requirements, so you may have to juggle this code about a bit, but you should be able to do what you want by setting the e.Handled property of the KeyEventArgs to false. According to the linked page, this:
Gets or sets a value that indicates the present state of the event handling for a routed event as it travels the route.
In plain English, that just means that we can stop it from being used any further. Try something like this:
private void TextBox_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter) // if Enter key is pressed...
{
// ... and Ctrl key is NOT... then ignore it
if (Keyboard.Modifiers != ModifierKeys.Control) e.Handled = true;
}
}

Activate a button Process via custom KeyPress

Basically in a C# Windows Form Application I am working on I have 2 buttons I press button1 and button2.
How do I make it when I press 2 custom keys simultaneously(eg. CTRL+L) the program does the steps coded for button1? Keeping in mind that the window might not be active.
I have looked at this: Keypress To Simulate A Button Click in C# but I don't think this would work if the window isn't active, and its also only one button pressed not two.
In Form KeyUp Event and the Properties is KeyPressPreview = true you can achieve this task.
private void Form_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.L && e.Control)
{
yourButton.PerformClick();
e.Handled = true;
}
}
or
if you want in Keypress Event
private void Form_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.Control && e.KeyCode == Keys.L)
{
yourButton.PerformClick();
e.Handled = true;
}
}
EDIT:
I realized that the OP want to the keypress Event from his statement "but I don't think this would work if the window isn't active"
You probably must check this
How to set a Windows hook in Visual C# .NET
If you precede any character in the button text with &, and keep UseMnemonic to true,. the alt+character will serve as shortcut key.
For example, in your case change the text of the buttons to button&1 and button&2 respectively. Keep UserMnemonic = true. Now if you press alt+1 then button1 will be pressed and if you press alt+2, button2 will be pressed.
Also 1 and 2 respectively will be underlined.
Hope that helps.

Windows Phone Keyboard Back-key press event?

I am working on a windows phone application.
I noticed that after I show up a textbox and a keyboard, if I press physical "back" key, the keyboard will hide, and press again it will go back to previous page.
But I want the page to navigate to previous page when the "back" key is press the first time. So its like the keyboard is up, when I press back key, it directly go back to the previous page.
I tried to implement the BackKeyPressed event handler of the page but it does not seem work, since it is not called when the first time back key is pressed.
Anyone has any good idea with this? thank you
You can implement a KeyUp eventhandler on the inputbox and then detect for Escape key event before calling NavigationService.GoBack(). Here is a piece of code to illustrate my point and the effect you are describing above:
The XAML (SecondPage.xaml):
<TextBox x:Name="SomeTextBox" KeyUp="SomeTextBox_KeyUp"/>
The C# Code-behind:
private void SomeTextBox_KeyUp(object sender, KeyEventArgs e)
{
if (e.Key == Key.Escape)
{
NavigationService.GoBack();
}
}
you can implement the LostFoucs event handler of theTextBox,navigate to the page which you want in the handler.

How to defocus a button when barcode scanner sent data ending with newline

I am writing a C# barcode application. I have a EAN-13 regex to detect barcodes in "Form1_KeyPress" function. I have no mechanism to detect where the input comes from. Here is my problem:
I have a reset button in the form which clears all fields and barcodes listed in a dataGridView. When I clicked on it, it gets focus as normal. When it has focus, if I read a barcode via barcode scanner, the newline at the end of each barcode reading causes this button to be clicked thus clearing all fields. So barcodes read are added to dataGridView but immediately deleted due to activation of reset button.
My current solution is to focus on a read-only textbox at the end of each "button_Click" function, but I don't want to write an irrelevant line at the end of each "click" function of buttons. What do you recommend? (by the way I cannot prevent surpress enter key in form's keydown function)
You can't capture the Enter key in the form's keystroke events because it is handled by the button.
If you add:
private void button_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
e.IsInputKey = true;
}
}
to a button then the Enter key won't cause the button to be clicked, and you will see a Form_KeyDown event for it.
You don't want to add this to every button, so create a simple UserControl which is just a button with this code added.
Update
This doesn't work for the space bar. If you set form.KeyPreview = true and add:
private void form_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Space)
{
e.Handled = true;
}
}
then the space bar won't press the button but it will still work in text boxes.
I don't know why Space and Enter behave differently.

Categories

Resources