Understanding multiple keys with WPF KeyDown event - c#

I'm using WPF KeyDown event. Can you please explain why this condition is true, when I press Ctrl+F1? When I press F1, Ctrl is already pressed so !Keyboard.IsKeyDown(Key.LeftCtrl) should be false.
Edit:
In the code below if you press Ctrl+F1 both messages would fire. But if you change the order of these two if statements, only "ctrlF1" message would fire like it should be. I would like to get explanation of that strange behavior.
private void Window_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.F1 && Keyboard.IsKeyDown(Key.LeftCtrl))
{
MessageBox.Show("ctrlF1");
}
if (e.Key == Key.F1 && !Keyboard.IsKeyDown(Key.LeftCtrl))
{
MessageBox.Show("F1");
}
}

The difference is as follows:
In the code you showed, when entering the handler, F1 is pressed and Ctrl is pressed (both conditions of first if-clause are true). MessageBox blocks the thread. Meanwhile you release the Ctrl key and click at the message. Then code execution goes on and Ctrl key is no longer pressed (both conditions of the second if-clause are true)
If you switch the if-statements, only the first condition (e.Key == Key.F1) of the first if-statement is true. Execution comes to second if-statement and both conditions are true. MessageBox is shown and execution stops till the MessageBox is closed.
The difference is: Pressing of the F1 key is evaluated before the handler is called, but the check Keyboard.IsKeyDown(Key.LeftCtrl) is evaluated in the moment, when the line of code is executed

Related

Assign a Keyboard key to a button click WINFORM

I am developing a small application with some buttons and textbox. What I am having problem is assigning a keyboard key (e.g. F3) to a button click.
For example if the user click the button Cash the code I wanted it's executed fine, but I want to make more easier instead clicking the button with mouse, I want the user be able to press the key on keyboard. I used the keydown event, also keypress event of that button, but still nothing.
I tried this keydown event
if (e.KeyCode == Keys.Enter)
{
btncash.PerformClick()
}
But still nothing
Do not use F3 function button it's used by OS for activating search. Enter key is fairs click event on focused control so do not use this also. Implement as suggested below.
In your Main form
Set KeyPreview to True in form load event.
Add KeyDown event handler with the following code
private void Form1_KeyDown(object sender,
KeyEventArgs e)
{
if (e.Control && e.KeyCode == Keys.H)
{
btncash.PerformClick();
//btncash_Click(null, null);
}
}

C# Ctrl+F doesn't work sometimes

I have a winforms application where in I have a textbox inside a form. I need to set focus to the textbox whenever I press 'Ctrl+F'. I have the following code in keyup event.
private void frm_KeyUp(object sender, KeyEventArgs e)
{
// Handle 'Ctrl + F' to Find
if (e.KeyData == (Keys.Control | Keys.F))
SetFocus();
}
Problem I have here is that sometimes, even though the focus is on form and I try 'Ctrl+F' the condition doesn't run. I know, as soon as I press 'Ctrl' the event gets fired even before I would have pressed key 'F'. Eventually it works, when I press both the keys at the very same time. So to the user it might look like the screen is unresponsive to the keys sometimes.
How can I overcome this situation?
You're using the KeyUp event and checking if the event contains both keys. This will only happen when you release both keys at the same time.
Change it to the KeyDown event instead, and check whether Ctrl was pressed at the moment F was pressed:
if (e.Control && e.KeyCode == Keys.F)
{
// ...
}

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.

KeyPress event only fires on certain keys in MonoDroid

I have a custom search activity that serves search suggestions. I get a reference to the search input box and set a handler:
mSearchInput = FindViewById(Resource.Id.searchBoxInput) as EditText;
mSearchInput.KeyPress += new System.EventHandler<View.KeyEventArgs>(OnSearchKeypress);
This is the handler that should be called on every key press:
private void OnSearchKeypress(object sender, View.KeyEventArgs e) {
string query = mSearchInput.Text;
if (e.KeyCode == Keycode.Enter && !string.IsNullOrEmpty(query)) {
e.Handled = true;
// launch search results activity
}
else {
e.Handled = false;
if (query.Length > 2) {
// load suggestions if we have 2+ characters
}
}
}
For some reason, on the soft keyboard, only certain key presses fire the event at all. Enter and Delete fire the event. Alphanumeric keys do not. Even if I take out all of the logic and simply do a Toast with e.KeyCode.ToString() it only shows the toast on those few keys.
Am I listening for the wrong event, is this a bug, or have I made some other mistake?
Device: Droid X2
Mono Version: 4.0.4
Update: I discovered that KeyPress is triggered twice for each physical press of a key: once for KeyEventActions.Up and once for Down. This doesn't explain why only certain keys fire the event but it should be noted that you need to check event.E.Action to find out whether the press or release is currently triggering the event.
When is the event not firing? If it is when the user is typing, then you can use the TextChanged event. You can then continue with your implementation in the OnKey method.
Also, look at this article for the "search" button:
http://www.stackoverflow.com/questions/1489852/android-handle-enter-in-an-edittext

Categories

Resources