Problems with the KeyPress, KeyDown and KeyUp events in C# - c#

So how to begin.
I am asking when you make a game for example and you add a controlls how do I make the form's key events run when the focus isnt on the form but on some of the controlls and when i call this.Focus() or this.Select() it doesnt't happen anything, but if I use a empty form(with no controls) it works(the events respond).And when i have for example 2 buttons and call button1.focus() and press a key the button1's event handler responds (only it) adn then when i call button2.Focus() it responds for button2.How is focus distributed througt the controlls?
I know for muttons you have to click them once to put the focus on them automaticly and for textboxtes too,but when i click the form it doesnt move the focus on the form.
Im have almost no experience with Key events please explayn me how to use them and how they function understandly.
PS: sorry for the long questin

You can set the Form.KeyPreview to true to have the form react to key events.

Related

How to set focus back to form, after button is pressed

I am making a game, and to open up and close the store, you press S. While in the store, you have six different choices to buy from, but they are all buttons.
However, once you buy something, the focus is no longer on the form, but on the button, and the key down event is part of the form, therefore, because the focus gets switched from the form to the button, the key down event no longer works, and disables you from closing the store and continuing on with the game.
My question is how to set the focus back to a form once a button is press? I started out with visual basic, and the code would be something along the lines of form1.setfocus, but its totally different in c#.
I have tried Activating the form, .focus, a lot, and nothing seems to be setting the focus back to the form. Help would be greatly appreciated.
Form1.focus();
But I think, to get keyboard events on Form itself, you need KeyPreview set to true for the Form so that Form gets Keys first and then other controls.
Try:
form.Focus();
MSDN:
The Focus method returns true if the control successfully received input focus. The control can have the input focus while not displaying any visual cues of having the focus. This behavior is primarily observed by the nonselectable controls listed below, or any controls derived from them.
Tell me more
You can add the key down event to the buttons too.

How to perform functions on keyboard press c#

I'm relatively new to C# and I want to know how can I press a key on my keyboard say the Q key and have it preform a list of functions?
I want to form a list of function under neath it. Anybody have any idea how to do this? thanks
//Something like this
if keyboard_down(Q)
{
//Do events here
}
I know this isn't the right function but that is basically what i want it to do. Winforms
You didn't mention which technology you work with.
I will assume that you are using Winforms.
In order to react to Key Press event you need to use Control.OnKeyPress - See here
See also this example which shows how to handle 'enter' key press
Depends on the way you want it.
Do you want them to fire while the form got focus or even without any focus to the form?
With focus:
Click your form in the form-designer and then go to the properties-tab. Click that little lightning bolt on top of it. There are a lot of events in that list. Double click "Keypress" and it will create an event automaticly and open the right code-snipped for you.
Depending on the focus you want, you can also mark controlls inside the form designer and then create the keypress event.
Without focus:
This would be a bit more difficultly. Here you would need to use a global keyboard hook to permanently grab pressed keys. Google for "c# keyboard hook". There are some examples out there.

DataGridViewTextBoxCell in edit mode steals keystrokes from Form even when using Form.KeyPreview

I have:
this.KeyPreview = true;
set in my Form, which allows me to typically receive KeyDown events even if a child element has focus. However, I have a DataGridView containing DataGridViewTextBoxCell's and when I click them, I enter "edit mode" for the cell and this somehow has such exclusive access to the keyboard that I can no longer receive these key presses. As long as anything in my entire Form has focus I want my keyboard functionality to actually work.
Does anyone know a way of getting these KeyDown events anyway?
Try handling the KeyUp event instead of KeyDown

textbox not losing focus

I have a Form which covers the entire screen. I have a textbox on it which is normally hidden but appears when a user clicks, drags mouse and then leaves. After that user can enter any value in the text box. Once entered, ideally user should be able to click outside of textbox and then the normal service should resume.
By normal service I mean that form should start getting all the events. What I have done so far is that on TextBox's KeyDown event; when Escape key is pressed, I have set the focus to the main form like this:
this.Focus(); //where this is mainform.
But this doesn't seem to work since Textbox still keeps receiving all the keys. I have a KeyDown event handler both for Form and Textbox and I have checked that all the KeyDown events pass on to the TextBox. I have a TextBox Leave event Handler as well which never gets called.
This TextBox is the only control on the form and the main form is used for drawing shapes (if that matters).
So, how can I make this TextBox lose focus when user clicks outside of it.
if it works like in VB, for what I remember, try to set the form property KeyPreview to false so all keys will be passed only to the focused control on the form.
If you set your form KeyPreview property to true, your form has first chance to handle any keystrokes that you make. If it is something you want to handle i.e. escape as in your comment above, handle it in your form's KeyDownEvent and mark it as handled so your textbox will not see it.
From above Msdn Page:
When this property is set to true, the form will receive all KeyPress, KeyDown, and KeyUp events. After the form's event handlers have completed processing the keystroke, the keystroke is then assigned to the control with focus.
I guess back in '11 this didn't work, but now
this.ActiveControl = null;
works fine. However if you intend to use Tab to cycle controls, focusing a label with suitable TabIndex is the way.

First key press lost when Windows Shell is replaced with Windows Forms application

I have succesfully replaced the Windows Shell following the approach given in this SO question.
However, I have detected that the first key press is lost and is a bit of an annoyance for the user. The solution we have tried is to activate the form in the OnShown event:
private void OnShownLoginForm(object z_sender, EventArgs z_e)
{
Activate();
m_loginTextBox.Focus();
}
But this hasn't solved the problem. Do you have any clue of what is happening?
You could try using the System.Windows.Forms.SendKeys Class (MSDN Documentation) to send a key press event to the form when in the Form Load event. If that doesn't help, try sending the keyboard events in the Form.Shown event since Form.Shown is the last event in the form start-up process.
Sounds like something caused by maybe another control getting focus first. Does the textbox have a taborder, and can you set it to 0? Focus should then be on it after the form loads.
Otherwise try creating a new form to test with, it really doesn't seem reproducible.
I do not know if it is related but I had a similar problem where the tabindex property of a webform did not work by pressing the TAB key, after focusing on the first input at page load, until the user first clicked on the form with the mouse.
I did not have access to the source code so I tried solving it with javascript.
Until the first mouse click, all keyboard strokes, including the TAB key, activated the keypress event, TAB key was undetected by keydown/keyup on page load.
I learned that the TAB key activated the keypress event and I could access the keycode through it. simply registering the keypress event and manually switching to the next input with jQuery worked.
after the first mouse click the form behaved as expected, TAB key was no longer caught by keypress event.
here is a code sample:
function tabNext(e){
if(e.keyCode == 9){
// do work
}
}
$('input').keypress(tabNext);

Categories