Setting custom hotkeys - c#

private void timer3_Tick(object sender, EventArgs e)
{
if (GetAsyncKeyState(Keys.LShiftKey) == -32767)
{
timer2.Start();
}
if (GetAsyncKeyState(Keys.LControlKey) == -32767)
{
timer2.Stop();
}
}
I use this for hot keys but is there a way for me to have a set button so a user could pick and set his own hotkey in the program instead of using a preset one.

Related

How can i make a hotkey in visual c#?

So what I'm trying to do is when I press a certain key, it will send keys. This is what I have so far. When I click F1 it doesn't do anything, and I'm not sure why.
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.F1)
{
SendKeys.Send("Example Text");
}
}
Try :
private void Form1_Load(object sender, EventArgs e)
{
this.KeyPreview = true;
this.KeyDown += new KeyEventHandler(Form1_KeyDown);
}
void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode.ToString() == "F1")
{
MessageBox.Show("F1 is pressed");
}
}

Windows Forms: set Tab as shortcut

There is no reaction on Tab shortcut in my app:
public class SomeWindow : Form {
// ...
private void someWindow_Load(object sender, EventArgs e) {
this.KeyPreview = true;
this.KeyDown += new KeyEventHandler(SomeWindow_KeyDown);
}
void SomeWindow_KeyDown(object sender, KeyEventArgs e) {
if (e.Control && e.KeyCode == Keys.B) {
// some actions
}
else if (e.KeyCode == Keys.Tab) {
// no reaction
}
}
}
I learned that Tab and direction keys are required some overrides, but all examples that I saw was related to textboxes. Any solutions for Tas as shortcut?

Keyup not working on custom theme called "Net Seal Theme"

Today I'm having a problem with the keyup event not working on a custom theme.
public Main()
{
InitializeComponent();
tbResolveS2I.KeyUp += new KeyEventHandler(tbResolveS2I_KeyUp);
tbResolveI2S.KeyUp += new KeyEventHandler(tbResolveI2S_KeyUp);
this.KeyPreview = true;
}
private void tbResolveS2I_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
btnResolveS2I_Click(sender, e);
e.SuppressKeyPress = true;
}
}
When I hit enter on my keyboard it sends both textboxes to do the same result. What i mean is if i hit enter on the first textbox it will act like I'm pressing enter on the other textbox aswell
If you want different events to perform the same thing you could add that code to a method and just call that method from the controls event method.
Also in the theme pack you are using, the button extends from Control and does not have a PerformClick() method.
example:
public Main() {
InitializeComponent();
KeyUp += new KeyEventHandler(Main_KeyUp);
this.KeyPreview = true;
}
private void Main_KeyUp(object sender, KeyEventArgs e) {
if(e.KeyCode == Keys.Enter) {
CustomCode();
e.SuppressKeyPress = true;
}
}
private void btnResolveS2I_Click(object sender, EventArgs e) {
CustomCode();
}
private void CustomCode() {
//Perform code here
}
}

Creating events on key stroke

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

KeyDown and keyUp event to avoid repeat letter in textbox control

My question is:
How can I disable the hold a key down in a textbox control using C#?
e.g Boxxxxxxxxxxxxxxxxxxxxx
I don't want to allow user to repeat any character from the keyboard.
Any suggestion?
thanks
Just an extra note for everyone using KeyDown and KeyUp events to suppress key repeat. If you do this you need to exclude catching meta keys like Alt/Shift etc otherwise when those keys are held down the actual key you want will not be sent since this is a two key combination and KeyDown and KeyUp events catch all keys.
This applies to all two-key combinations.
I did not add all of the meta keys to the following example, just the most common ones.
You can easily add your own keys by adding them to the collection.
Extending on the post from BFree:
public partial class Form1 : Form
{
private static readonly System.Collections.Generic.ICollection<System.Windows.Forms.Keys) ExcludeKeys = new System.Collections.Generic.HashSet<System.Windows.Forms.Keys)()
{
System.Windows.Forms.Keys.None,
System.Windows.Forms.Keys.Shift,
System.Windows.Forms.Keys.ShiftKey,
System.Windows.Forms.Keys.LShiftKey,
System.Windows.Forms.Keys.RShiftKey,
System.Windows.Forms.Keys.Alt,
System.Windows.Forms.Keys.Control,
System.Windows.Forms.Keys.ControlKey,
System.Windows.Forms.Keys.LControlKey,
System.Windows.Forms.Keys.RControlKey,
System.Windows.Forms.Keys.CapsLock,
System.Windows.Forms.Keys.NumLock,
System.Windows.Forms.Keys.LWin,
System.Windows.Forms.Keys.RWin
}
private bool isKeyPressed = false;
public Form1()
{
InitializeComponent();
this.textBox1.KeyDown += new KeyEventHandler(textBox1_KeyDown);
this.textBox1.KeyUp += new KeyEventHandler(textBox1_KeyUp);
}
void textBox1_KeyUp(object sender, KeyEventArgs e)
{
if (!ExcludeKeys.Contains(e.KeyCode))
{
isKeyPressed = false;
}
}
void textBox1_KeyDown(object sender, KeyEventArgs e)
{
if (!ExcludeKeys.Contains(e.KeyCode))
{
e.SuppressKeyPress = isKeyPressed;
isKeyPressed = true;
}
}
}
You can use the SupressKeyPress property on KeyEventArgs:
public partial class Form1 : Form
{
private bool isKeyPressed = false;
public Form1()
{
InitializeComponent();
this.textBox1.KeyDown += new KeyEventHandler(textBox1_KeyDown);
this.textBox1.KeyUp += new KeyEventHandler(textBox1_KeyUp);
}
void textBox1_KeyUp(object sender, KeyEventArgs e)
{
isKeyPressed = false;
}
void textBox1_KeyDown(object sender, KeyEventArgs e)
{
e.SuppressKeyPress = isKeyPressed;
isKeyPressed = true;
}
}
bool prevent = false;
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
e.SuppressKeyPress = prevent;
}
private void textBox1_KeyUp(object sender, KeyEventArgs e)
{
prevent = false;
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
prevent = true;
}
I'm not familiar with the specifics in c# but I imagine something like:
while (keyIsDown){
//do nothing
}
would work. I would be surprised if there is no way to keep track of a key being pressed.

Categories

Resources