I want to create an event when every key stroke on the keyboard happened. So far I did this:
namespace WindowsFormsApplication1
{
public partial class SpaceInvadors : Form
{
SpaceInvadors sInvaders;
public SpaceInvadors()
{
InitializeComponent();
KeyStroke keyStroke = new KeyStroke();
sInvaders=new SpaceInvadors(keyStroke);
}
public SpaceInvadors(KeyStroke keyStroke)
{
keyStroke.keyStroked += Form1_KeyDown;//How does that chaining to the event work?
keyStroke.keyStroked += Form1_KeyUp;
}
private void Form1_KeyDown(object sender, KeyArgs e)
{
if (e.KeyCode == Keys.Q)
{
Application.Exit();
}
}
}
public class KeyStroke
{
public event EventHandler<KeyArgs> keyStroked;
public void Add(Keys key)
{
// I need to know how to obtain this key:
KeyArgs keyAr = new KeyArgs(key);
keyRisen(keyAr);
}
public void keyRisen(KeyArgs kA)
{
if (keyStroked != null)
keyStroked(this, kA);
}
}
public class KeyArgs : EventArgs
{
public Keys KeyCode { get; set; }
public KeyArgs(Keys key)
{
KeyCode = key;
}
}
Also, how can I all the time listen to what keys were pressed, and not only once
For your question
"Also, how can I all the time listen
to what keys were pressed, and not
only once"
I see that you have already used Form.KeyDown event to close your game if 'q' was pressed:
private void Form1_KeyDown(object sender, KeyArgs e)
{
if (e.KeyCode == Keys.Q)
{
Application.Exit();
}
}
Ok you can add more keys to your method:
private void Form1_KeyDown(object sender, KeyArgs e)
{
if (e.KeyCode == Keys.Q)
{
Application.Exit();
}
else if (e.KeyCode == Keys.Up)
spaceInvaders.MoveUp();
}
For the chaining, I think that class KeyStoke manage playing interface. Because KeyDown event is used to determine what key was pressed. I advise you more learning to events and delegates.
If I understand correct you are writing a game, The space invaders! :)
I dont think that this approach is good for game. The most appropriate and commonly used approach is to use GetKeyboardState function. To achive good ingame results with this function you should create timer and check key states periodically. And I recomment you perform rendering also in timer routine.
If some reasons prevents you to use GetKeyboardState I will recomend you to use PreviewKeyDown
Related
So, apparently I had some problem when handling keys such as F10 or F11.
I want to move the focus from current textbox into another textbox, but not in one particular textbox. So, I wrote some code to handle key:
private void checkKeys(KeyEventArgs e)
{
if (e.KeyCode == Keys.F10)
{
buyerName.Focus();
}
else if (e.KeyCode == Keys.F11)
{
discount.Focus();
}
}
But, if I put this into individual textbox, which kinda hassle to me. Is there any method to listen key whether in global userControl or textbox?
Edit : here's my structure that I want to ask :
Form-
|-User Control
|-TextBox
Edit 2 : here's some image might help img
To use a global keyboard listener in Winforms, you just need to add a handler to KeyUp action for the main form itself:
private void Form1_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.F10)
{
textBox1.Focus();
e.Handled = true; //To use F10, you need to set the handled state to true
} else if (e.KeyCode == Keys.F11)
{
textBox2.Focus();
}
}
Then make sure that the KeyPreview property on the main form is set to True.
The issue with the application freezing when pressing F10 is because it is waiting for another consecutive action. To bypass this simply set the Handled property on the keyevent to TRUE. This releases the unresolved event.
This is my entire form class, refactored to use a helper method as you are refering to. This works fine. But you have to make sure that the KeyPreview property on your form is True, unless your keypresses will not be matched to your event handlers.
namespace KeyTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void Form1_KeyUp(object sender, KeyEventArgs e)
{
CheckKeys(e);
}
private void CheckKeys(KeyEventArgs e)
{
if (e.KeyCode == Keys.F10)
{
textBox1.Focus();
e.Handled = true;
}
else if (e.KeyCode == Keys.F11)
{
textBox2.Focus();
e.Handled = true;
}
}
}
}
Now in your comment you are mentioning a UserControl, if you want that, then you need to create an instance method on your UserControl class, and pass the event to that from your global keyboard event handler on your main form.
public partial class UserControl1 : UserControl
{
public UserControl1()
{
InitializeComponent();
}
public void HandleKeys(KeyEventArgs e)
{
if (e.KeyCode == Keys.F10)
{
textBox1.Focus();
e.Handled = true;
}
else if (e.KeyCode == Keys.F11)
{
textBox2.Focus();
e.Handled = true;
}
}
}
Then on your main form:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void Form1_KeyUp(object sender, KeyEventArgs e)
{
CheckKeys(e);
}
private void CheckKeys(KeyEventArgs e)
{
uc1.HandleKeys(e); //Instance method on your user control.
}
}
This then works as intended.
As pointed out in one of the comments, a better way would be to override the ProcessCmdKey method on the Form base class. This would be done like so:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
userControl11.HandleKeys(keyData); // method on the userControl to handle the key code.
base.ProcessCmdKey(ref msg, keyData);
return true;
}
}
The handler on the UserControl stays more or less the same:
public void HandleKeys(Keys keys)
{
if (keys == Keys.F10)
{
nameTB.Focus();
} else if (keys == Keys.F11)
{
emailTB.Focus();
}
}
Whether this is a more correct way of doing it, I am unsure of. They certainly both accomplish the same result. The documentation shows the first method in for handling keyboard events at the form level here:
How to handle keyboard input
But states here that the ProcessCmdKey method is to provide additional handling of shortcuts and MDI accellerators.
ProcessCmdKey
I will leave that up to you to decide what is the best for your scenario. But keep it in to show how you would use it should you choose to.
You can hook up to the KeyUp event of your form.
That way, any key pressed while your form is focused will be send to you (if the control didn't handle the key).
Thanks to #Espen and #reza-aghaei for handling keys into main form. Unfortunately, I still didn't managed find a way to focus to designated textbox inside a UserControl. However, I make some dirty method which kinda crappy and un-efficient by searching child control from it's parent
//MainForm.cs
if(yourUserControl.Name)//Do some check for targeted userControl, if null can cause NullReferenceException
{
if (e.KeyCode == Keys.F10)
{
this.Controls.Find("textboxName", true).First().Focus();
e.Handled = true;
}
}
Is there a way to distinguish whether the Enter event on a control was raised by keyboard (Tab, Shift+Tab) or by direct mouse click?
I need to perform an action only when the user is moving to the control using Tab, but not when the user directly clicks on the control. I have tried to intercept the mouse click directly, but it seems the Enter event is raised before Click.
Instead of tracking the Tab key, you can use the WM_MOUSEACTIVATE message to detect activation of the control with the mouse. You could either sub-class each control type you use and override the WndProc method or use a NativeWindow listener class like the one presented below. Depending on how many types of controls you use, it may be less work and clutter to just sub-class those controls to provide a property that indicates that the control was selected using the mouse. It is your decision to make, but the pattern will be the same.
This code is a slight modification of the example shown in the MS documentation.
public class MouseActivateListener : NativeWindow
{
private Control parent;
public MouseActivateListener(Control parent)
{
parent.HandleCreated += this.OnHandleCreated;
parent.HandleDestroyed += this.OnHandleDestroyed;
parent.Leave += Parent_Leave;
this.parent = parent;
if (parent.IsHandleCreated)
{
AssignHandle(parent.Handle);
}
}
private void Parent_Leave(object sender, EventArgs e)
{
MouseActivated = false;
}
private void OnHandleCreated(object sender, EventArgs e)
{
AssignHandle(((Form)sender).Handle);
}
private void OnHandleDestroyed(object sender, EventArgs e)
{
ReleaseHandle();
}
public bool MouseActivated { get; set; }
[System.Security.Permissions.PermissionSetAttribute(System.Security.Permissions.SecurityAction.Demand, Name = "FullTrust")]
protected override void WndProc(ref Message m)
{
const Int32 WM_MouseActivate = 0x21;
base.WndProc(ref m);
if (m.Msg == WM_MouseActivate && m.Result.ToInt32() < 3)
{
MouseActivated = true;
}
}
}
Example Usage:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private MouseActivateListener textBox1Listener;
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
textBox1Listener = new MouseActivateListener(textBox1);
}
private void textBox1_Enter(object sender, EventArgs e)
{
if (textBox1Listener.MouseActivated)
{
MessageBox.Show("Mouse Enter");
}
else
{
MessageBox.Show("Tab Enter");
}
}
}
You can use the Form.KeyPreview event and store the last key press in a variable. Then in your control's Enter event, check the value of the key that was pressed last. If this is a tab, do whatever you need to:
private Keys lastKeyCode;
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
this.lastKeyCode = e.KeyCode;
}
Then in the Enter event, check it:
if (lastKeyCode == Keys.Tab)
{
// Whatever...
}
Intercepting WM_KEYUP and WM_KEYDOWN directly with a message filter to retrieve the state of the Tab key worked. This seems excessive for such a seemingly straightforward task, but apparently the Tab key is suppressed from most windows forms events.
Would be happy to take a cleaner answer, but for now, this is it:
class TabMessageFilter : IMessageFilter
{
public bool TabState { get; set; }
public bool PreFilterMessage(ref Message m)
{
const int WM_KEYUP = 0x101;
const int WM_KEYDOWN = 0x100;
switch (m.Msg)
{
case WM_KEYDOWN:
if ((Keys)m.WParam == Keys.Tab) TabState = true;
break;
case WM_KEYUP:
if ((Keys)m.WParam == Keys.Tab) TabState = false;
break;
}
return false;
}
}
class MainForm : Form
{
TabMessageFilter tabFilter;
public MainForm()
{
tabFilter = new TabMessageFilter();
Application.AddMessageFilter(tabFilter);
}
protected override void OnFormClosed(FormClosedEventArgs e)
{
Application.RemoveMessageFilter(tabFilter);
base.OnFormClosed(e);
}
void control_Enter(object sender, EventArgs e)
{
if (tabFilter.TabState) // do something
else // do domething else
}
}
I am currently working on an app that uses the tool "TimePicker" in Blend for Visual Studio 2017 and I have a question regarding an event I'm trying to find. After a time is selected I wish to have separate events to occur when check-button is pressed that changes the time and if the X-button is pressed to cancel and return from the flyout. I do believe I can use the TimePicker's TimeChanged event to tell whether or not the check button was pressed but I cannot figure out which event would work for if the x-button to cancel was pressed. Does anyone know?
There's no obvious way to do it. There's a Dismiss button in the TimePickerFlyoutPresenter, but getting access to that is not enough. You'll also need to manage keyboard shortcuts, like the [ESC] key.
You got me a bit curious on how to hack it, so here's what I've come up with so far. Here's a TimePickerDismissal class I hacked-up real quick. The idea is if the TimePicker gains focus after it pushed a popup and it did not report a time change, then it's considered a dismiss.
public class TimePickerDismissal
{
private bool _active;
private bool _timeChanged;
public event EventHandler Dismissed;
public TimePickerDismissal(TimePicker timer)
{
timer.GotFocus += OnTimeGotFocus;
timer.LostFocus += OnTimeLostFocus;
timer.TimeChanged += OnTimeChanged;
}
private void OnTimeGotFocus(object sender, RoutedEventArgs e)
{
if (!_active)
{
return;
}
_active = false;
if (!_timeChanged)
{
Dismissed?.Invoke(this, EventArgs.Empty);
}
_timeChanged = false;
}
private void OnTimeLostFocus(object sender, RoutedEventArgs e)
{
var selector = FocusManager.GetFocusedElement() as LoopingSelector;
if (selector == null)
{
return;
}
_active = true;
}
private void OnTimeChanged(object sender, TimePickerValueChangedEventArgs e)
{
_timeChanged = true;
}
}
Here's how to use it:
public sealed partial class MainPage
{
public MainPage()
{
InitializeComponent();
var dismissal = new TimePickerDismissal(MyTimePicker);
dismissal.Dismissed += OnTimerDismissed;
}
private void OnTimerDismissed(object sender, EventArgs e)
{
Debug.WriteLine("TimePicker dismissed!");
}
}
Give that a shot. Let me know if it works for you. You can probably turn this into a behavior actually...
I have a UserControl with some controls and a textbox on it, i want to do some procedures when the Enter key hits on that textbox, I have this code in the UserControl:
public event EventHandler TextBoxKeyPressed
{
add { textBox.KeyPressed+=value; }
remove { textBox.KeyPressed-=value; }
}
in my main form I have a lot of this control and for each one I should check for the key pressed and if it was Enter key then do the procedures.
Is there any way to create a custom event to check the key pressed in the UserControl and if it was Enter Key then fire that event?
Update: each custom control may have different procedures on KeyPresssd event
Sure, you can just add, say, an EnterPressed event and fire it when you detect that the Enter key was pressed:
public partial class UserControl1 : UserControl {
public event EventHandler EnterPressed;
public UserControl1() {
InitializeComponent();
textBox1.KeyDown += textBox1_KeyDown;
}
protected void OnEnterPressed(EventArgs e) {
var handler = this.EnterPressed;
if (handler != null) handler(this, e);
}
void textBox1_KeyDown(object sender, KeyEventArgs e) {
if (e.KeyCode == Keys.Enter) {
OnEnterPressed(EventArgs.Empty);
e.Handled = e.SuppressKeyPress = true;
}
}
}
The event doesn't change, it would still be a normal key pressed event. You'd simply only perform the intended action therein if the key was the enter key. Something like this:
private void textBox_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)Keys.Return)
{
// the enter key was pressed
}
}
Okay so I created a method called KeyCheck(), that should check if a key is pressed (specifically the enter key) and if it is it will press button1.
Unfortunately when I call the method I am unsure what to pass along to it. I want it to know when the enter key is being pressed.
public partial class Form1 : Form
{
public void GameStart()
{
richTextBox1.WordWrap = true;
richTextBox1.SelectionAlignment = HorizontalAlignment.Center;
richTextBox1.Text = "Hello, Welcome to Grandar!";
}
public Form1()
{
InitializeComponent();
GameStart();
//What variable do I pass to KeyCheck Method?
KeyCheck();
}
private void KeyCheck(KeyPressEventArgs k)
{
if (k.KeyChar == (char)Keys.Enter)
{
button1.PerformClick();
}
}
private void richTextBox1_TextChanged(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
}
}
Couple of things to note here:
a) Do you really want to be calling KeyCheck directly as your sample code suggests or should it be wired up as a handler on the form (where the info you're asking will be automatically provided - will require a change in signature to align with the standard handlers as you have in some of your other methods).
b) I don't think you'll be able to call the KeyCheck method like you're doing unless you hook up another event to capture the keypress and then pass it to this method, by newing up a new KeyPressEvent(...)
Therefore, to answer your question, I think you'll want something like (pseudo-code)
public Form1()
{
InitializeComponent();
GameStart();
// Wire up a handler for the KeyPress event
this.KeyPress += KeyCheck;
}
private void KeyCheck(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)Keys.Enter)
{
button1.PerformClick();
}
}
Check out this page: https://msdn.microsoft.com/en-us/library/system.windows.forms.control.keypress(v=vs.110).aspx
You'll want something similar to your other methods, with the sender object and the event args.
if (e.KeyCode < Keys.Enter) {
//Your logic
}
subscribe to this:
this.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.KeyPress_Method);
and the method to check the Enter key:
void KeyPress_Method(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)13) // enter key
{
// your code
}
}