How do you check if Mouse 4/5 have been pressed? - c#

So I want to make a Global hotkey and have Mouse 4 or 5 auto click my mouse.
The way I'm global binding is a bit interesting.
So on the form, I have a label that will set active control when clicked to listen to show what the keybind is when pressed.
private Keys clickerHotkey;
private void leftClickHotkeyLbl_Click(object sender, EventArgs e)
{
this.ActiveControl = leftClickHotkeyLbl; // set active to able to respond to the key down event.
leftClickHotkeyLbl.Text = "[...]";
}
private void leftClickHotkeyLbl_KeyDown(object sender, KeyEventArgs e)
{
leftClickHotkeyLbl.TabStop = false;
if (!((e.KeyValue >= 16 && e.KeyValue <= 18) || (e.KeyValue >= 21 && e.KeyValue <= 25) || (e.KeyValue >= 28 && e.KeyValue <= 31) || e.KeyValue == 229 || (e.KeyValue >= 91 && e.KeyValue <= 92))) // this gets rid of non sense keys...
{
KeyBindManager.KeysConverter.UnregisterHotKey(this.Handle, (int)clickerHotkey); // unregister previous key.
clickerHotkey = e.KeyData;
if (clickerHotkey == Keys.XButton1) // doesn't work :(
{
Console.WriteLine("Mouse 5 Detected");
}
if (clickerHotkey == Keys.Escape) // if the key is escape, return
{
UnsetHotkey(clickerHotkey);
leftClickHotkeyLbl.Text = "[-]";
this.ActiveControl = null;
return;
}
clickerModifiers = ExtractModifier(clickerModifiers, e);
SetHotkey(clickerModifiers, clickerHotkey);
leftClickHotkeyLbl.Text = $"[{KeyBindManager.KeysConverter.Convert(clickerHotkey)}]";
this.ActiveControl = null; // set to null so its no longer being edited.
}
}
Any help is much appreciated! Cheers!

You are trying to detect a mouse event/mouse button via a keyboard key...
You cant use Keys.Button for a mouse event as it's specific to keyboard keys, chence the name Keys.
if (clickerHotkey == Keys.XButton1) // doesn't work :(
{
Console.WriteLine("Mouse 5 Detected");
}
If you want to detect a mouse event/mouse button click you can change it to the following:
if (clickerHotkey == MouseButtons.XButton1) // or you can use XButton2
{
Console.WriteLine("Mouse 5 Detected");
}
This intern specifies that you want to listen for a mouse clicks.
Due to your event handler being a Keyboard specific one, you need to add a seperate event handler for mouse clicks eg.
private void mouseClick(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.XButton1)
{
Console.WriteLine("Mouse 5 Detected");
}
}

Related

WPF: Change Button Text when SHIFT is pressed

I would like to change the text (Content property) of a button when the SHIFT key is pressed. In that cases the button shall execute a different command. That is a common UI behaviour e. g. in Photoshop.
Any idea how to do this.
Many thanks in advance
Add the KeyDown or PreviewKeyDown event to your Button element.
<Button Width="300" Height="50" Name="btnFunction" KeyDown="btnFunctionKeyDown" Content="Function1"/>
And the C# Code:
private void btnFunctionKeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.LeftShift || e.Key == Key.RightShift)
{
btnFunction.Content = "Function2";
}
}
Have a look on this Article for more information:
https://learn.microsoft.com/de-de/dotnet/api/system.windows.input.keyboard.keydown?view=netcore-3.1
Here my solution (event is handled at the Window) - many thanks for your input - if there is a better solution kindly comment...
internal void HandlePreviewKeyDown(KeyEventArgs e)
{
IInputElement focusedControl = FocusManager.GetFocusedElement(_window);
if (( (Keyboard.Modifiers & ModifierKeys.Shift) == ModifierKeys.Shift) && !(focusedControl?.GetType() == typeof(TextBox)))
{
// set button text
e.Handled = true;
}
}
internal void HandlePreviewKeyUp(KeyEventArgs e)
{
IInputElement focusedControl = FocusManager.GetFocusedElement(_window);
if ( (e.Key == Key.LeftShift) || (e.Key == Key.RightShift) && !(focusedControl?.GetType() == typeof(TextBox)))
{
// re-set button text
e.Handled = true;
}
}

How to make a picturebox rotate only once in c#?

I'm making a mini game. I want to rotate my player with this code when I turn left and right picPlayer.Image.RotateFlip(RotateFlipType.RotateNoneFlipX) My moving code is:
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
//when one of the movement keys are pressed,
//makes it's variable true.
if (e.KeyCode == Keys.Right || e.KeyCode == Keys.D)
{
moveRight = true;
}
}
Also, my timer's program is:
private void tmrMovementPlayer_Tick(object sender, EventArgs e)
{
//whenever right arrow is pressed,
if (moveRight == true)
{
//decrease the x variable by 5 (moves right)
x = x + PLAYER_SPEED;
//check for boundaries (if the player is out of the screen)
if (x >= this.ClientSize.Width - picPlayer.Width)
{
//if yes, set it back to the boundary.
x = this.ClientSize.Width - picPlayer.Width;
}
//check the subprogram for info
MovePlayer();
}
}
What should I do at this point? Thanks.
So thanks to the other answer, I found the solution. It was a little bit different. I created another boolean called "goingRight" just for the rotation.
if (e.KeyCode == Keys.Right || e.KeyCode == Keys.D)
{
//make move right true
moveRight = true;
//if i was going left,
if (goingRight == false)
{
//say it im going right
goingRight = true;
//and flip it (only if i was going left before)
picPlayer.Image.RotateFlip(RotateFlipType.RotateNoneFlipX);
}
}
Again, thanks for all the help.
Try something like...
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
//when one of the movement keys are pressed,
//makes it's variable true.
if (e.KeyCode == Keys.Right || e.KeyCode == Keys.D)
{
if (!moveRight)
{
picPlayer.Image.RotateFlip(RotateFlipType.RotateNoneFlipX)
moveRight = true;
}
}
}

Trying to detect keypress

I made a method that detects when a key is pressed, but its not working! Heres my code
void KeyDetect(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.W && firstload == true)
{
MessageBox.Show("Good, now move to that box over to your left");
firstload = false;
}
}
I also tried to make a keyeventhandler but, it sais "cannot assign to key detect because it is a method group"
public Gwindow()
{
this.KeyDetect += new KeyEventHandler(KeyDetect);
InitializeComponent();
}
Use keypress event like this:
private void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyCode == Keys.F1 && e.Alt)
{
//do something
}
}
1) Go to your form's Properties
2) Look for the "Misc" section and make sure "KeyPreview" is set to "True"
3) Go to your form's Events
4) Look for the "Key" section and double click "KeyDown" to generate a function to handle key down events
Here is some example code:
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
Console.WriteLine("You pressed " + e.KeyCode);
if (e.KeyCode == Keys.D0 || e.KeyCode == Keys.NumPad0)
{
//Do Something if the 0 key is pressed (includes Num Pad 0)
}
}
You are looking for this.KeyPress. See How to Handle Keypress Events on MSDN.
Try to use the KeyDown event.
Just see KeyDown in MSDN
Just do
if (Input.GetKeyDown("/* KEYCODE HERE */"))
{
/* CODE HERE */
}

How can I determine in KeyDown that Shift + Tab was pressed

How can I determine in KeyDown that ⇧ + Tab was pressed.
private void DateTimePicker_BirthDate_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Tab && e.Modifiers == Keys.Shift)
{
//do stuff
}
}
can't work, because never both keys are pressed exactly in the same second. You always to at first the Shift and then the other one..
It can't work, because never both keys are pressed exactly in the same second.
You're right that your code doesn't work, but your reason is wrong. The problem is that the Tab key has a special meaning - it causes the focus to change. Your event handler is not called.
If you use a different key instead of Tab, then your code will work fine.
If you really want to change the behaviour of Shift + Tab for one specific control, it can be done by overriding ProcessCmdKey but remember that many users use the Tab key to navigate around the form and changing the behaviour of this key may annoy those users.
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (DateTimePicker_BirthDate.Focused && keyData == (Keys.Tab | Keys.Shift))
{
MessageBox.Show("shift + tab pressed");
return true;
}
else
{
return base.ProcessCmdKey(ref msg, keyData);
}
}
If you are looking for a key press combination (Tab, then Shift) like Ctrl K + D you will have to use this modified example which was taken from MSDN social.
private StringBuilder _pressedKeys = new StringBuilder();
private void DateTimePicker_BirthDate_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Tab)
{
_pressedKeys.Append("Tab");
return;
}
if (e.Modifiers == Keys.Shift)
{
_pressedKeys.Append("Shift");
return;
}
if (_pressedKeys.ToString()."TabShift")
{
MessageBox.Show("It works!");
_pressedKeys.Clear();
}
else
{
_pressedKeys.Clear();
}
base.OnKeyDown(e);
}
First hook the Tab keypress event, then during the event, check the state of the Shift key. Keep in mind that there are two shift keys; make sure you check both of them.
This very related post shows how to check the state of modifier keys:
How to detect the currently pressed key?
Edit: an insight provided by another answerer who justly deserves an upvote is that the default behavior of the tab key (to change control focus) must be suppressed.
You can find your answer in
this post
It's Simple.
You can do that using KeyUp Event in the TextBox
private void txtBox1_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Tab && e.Shift == false) // TAB Key Pressed
{
}
if (e.KeyCode == Keys.Tab && e.Shift == true) // TAB + SHIFT Key Pressed
{
}
}
Or
Using this you can identify Any Key is press inside the form
//Add This code inside the Form_Load Event
private void Form1_Load(object sender, EventArgs e)
{
this.KeyUp += new System.Windows.Forms.KeyEventHandler(KeyPressEvent);
this.KeyPreview = true;
}
//Create this Custom Event
private void KeyPressEvent(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Tab && e.Shift == false) // TAB Key Pressed
{
}
if (e.KeyCode == Keys.Tab && e.Shift == true) // TAB + SHIFT Key Pressed
{
}
}
It's Simple.
Using this you can identify Any Key is press inside the form
//Add This code inside the Form_Load Event
private void Form1_Load(object sender, EventArgs e)
{
this.KeyUp += new System.Windows.Forms.KeyEventHandler(KeyPressEvent);
this.KeyPreview = true;
}
//Create this Custom Event
private void KeyPressEvent(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Tab && e.Shift == false) // TAB Key Pressed
{
}
if (e.KeyCode == Keys.Tab && e.Shift == true) // TAB + SHIFT Key Pressed
{
}
}

How to detect multiple keys entered in c# keydown event of textbox?

I want to design a numeric textbox in silverlight.
I have added keydown event of TextBox to handle the keypress.
Inside event I validate the key entered in the textbox.
event as follows
private void TextBox_KeyDown(object sender, KeyEventArgs e)
{
if (!this.Validate(sender,e))
e.Handled = true;
}
function Validate as follows
private bool Validate(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter) //accept enter and tab
{
return true;
}
if (e.Key == Key.Tab)
{
return true;
}
if (e.Key < Key.D0 || e.Key > Key.D9) //accept number on alphnumeric key
if (e.Key < Key.NumPad0 || e.Key > Key.NumPad9) //accept number fomr NumPad
if (e.Key != Key.Back) //accept backspace
return false;
return true;
}
I am not able to detect shift and Key.D0 to Key.D1 i.e.
SHIFT + 1 which returns "!"
SHIFT + 3 returns "#" like wise any other special keys.
I dont want user to enter special character in to textbox.
How do i handle this keys event??
In Silverlight, I don't think there are any Modifiers in the KeyEventArgs class but instead in Keyboard:
public void KeyDown(KeyEventArgs e)
{
if (Keyboard.Modifiers == ModifierKeys.Control && e.Key == Key.D0)
{
ControlZero();
}
}
e should have a property on it that will tell you if Shift, Control or Alt are pressed. e.Modifiers can also give you additional information about which additional modifier keys have been pressed.
To cancel the character you can set e.Handled to True, which will cause the control to ignore the keypress.
textBox2.Text = string.Empty;
textBox2.Text = e.Modifiers.ToString() + " + " + e.KeyCode.ToString();

Categories