Perform the action when the key is pressed - c#

I am working on my project and i need to add an action that is only performed while left CTRL is pressed
This is my code:
private void Izrada_kartice_KeyDown(object sender,KeyEventArgs e)
{
if(e.Control)
{
promijeni_veličinu_naslov = true;
this.BackColor = Color.Red;
}
}
private void Izrada_kartice_KeyUp(object sender, KeyEventArgs e)
{
if (e.Control)
{
promijeni_veličinu_naslov = false;
this.BackColor = Color.Green;
}
}
Now when I run this code and press CTRL all is OK, but when I release CTRL
nothing happened.
I was try this:
private void Izrada_kartice_KeyDown(object sender,KeyEventArgs e)
{
if(e.KeyCode == Keys.A)
{
promijeni_veličinu_naslov = true;
this.BackColor = Color.Red;
}
}
private void Izrada_kartice_KeyUp(object sender, KeyEventArgs e)
{
if(e.KeyCode == Keys.A)
{
promijeni_veličinu_naslov = false;
this.BackColor = Color.Green;
}
}
This works fine but I need do this with CTRL

The KeyUp event will fire when you release Ctrl, but the e.Control boolean is not set to True because Ctrl is no longer being held down.
In short: Don't detect the release of Ctrl by inspecting e.Control, inspect the e.KeyCode instead; it will be Keys.ControlKey
if(e.KeyCode == Keys.ControlKey)
{
...
}

On your second code, replace Keys.A with Keys.ControlKey like this:
private void Izrada_kartice_KeyDown(object
sender,KeyEventArgs e)
{
if(e.KeyCode == Keys.ControlKey)
{
promijeni_veličinu_naslov = true;
this.BackColor = Color.Red;
}
}
private void Izrada_kartice_KeyUp(object sender,
KeyEventArgs e)
{
if(e.KeyCode == Keys.ControlKey)
{
promijeni_veličinu_naslov = false;
this.BackColor = Color.Green;
}
}

Related

why can't I use keybinds outside the program?

I'm try make program to open other apps, to open this program i wan't use keybinds and for hide too.
private void Window_KeyDown(object sender, KeyEventArgs e)
{
if (Dark == 0)
{
if (e.Key == Key.LeftCtrl)
{
if (e.Key == Key.LWin)
{
this.Topmost = true;
Dark = 1;
}
}
}
else if(Dark == 1)
{
if (e.Key == Key.LeftCtrl)
{
if (e.Key == Key.LWin)
{
this.Topmost = false;
Dark = 0;
WindowState = WindowState.Minimized;
}
}
else if (e.Key == Key.Escape)
{
this.Topmost = false;
Dark = 0;
WindowState = WindowState.Minimized;
}
}
Outside the program, keybinds do not work at all, but if the program is open, then they work, why?
you can refer to my class, used to global hook key event
https://github.com/nhochjkaru/JEOrbwalk/blob/master/UserActivityHook.cs
declare: UserActivityHook actHook;
Main function:
actHook = new UserActivityHook();
actHook.KeyDown += new KeyEventHandler(Window_KeyDown);

How to catch key states when I am dragging an object over a control? - C#

I need to change drag&drop effects with some keys while I am dragging some text inside a listbox.
bool ctrlD = false;
private void MainForm_KeyDown(object sender, KeyEventArgs e)
{
if (e.Control && e.KeyCode == Keys.D)
ctrlD = true;
}
// KeyUp
private void textBox_MouseDown(object sender, MouseEventArgs e)
{
textBox.DoDragDrop(textBox.Text, DragDropEffects.All);
}
private void listBox_DragOver(object sender, DragEventArgs e)
{
if (ctrlD) e.Effect = DragDropEffects.Copy;
else e.Effect = DragDropEffects.Move;
}
The problem is DragOver method doesn't see when any keys are pressed. Effects don't change. What can I do to this?
bool ctrlD = false
Use object sender
private void textBox_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
this.DoDragDrop(sender, DragDropEffects.All);
}
}
Make sure to set AllowDrop to True
Add (MyContol) (_DragEnter) and (_DragDrop) events
private void MyControl_DragEnter(object sender, DragEventArgs e)
{
/*
DragDropEffects
*/
e.Effect = DragDropEffects.Copy;
}
private void MyControl_DragDrop(object sender, DragEventArgs e)
{
TextBox tb = e.Data.GetData(typeof(TextBox)) as TextBox;
/*
MyControl.Controls.Add(tb);
* Your code here
*/
}
You need to read the DragEventArgs within your listBox_DragOver event to know what buttons and keys have been used for the drag operation:
if ((e.KeyState & 32) == 32) { bool alt = true; }
if ((e.KeyState & 16) == 16) { bool mousebuttonmiddle = true; }
if ((e.KeyState & 8) == 8) { bool control = true; }
if ((e.KeyState & 4) == 4) { bool shift = true; }
if ((e.KeyState & 2) == 2) { bool mousebutton2 = true; }
if ((e.KeyState & 1) == 1) { bool mousebutton1 = true; }

connect a single key event to the control that has focus

I have two textboxes, each with its own keypress event attached.
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if(e.KeyChar == '\r')
{
e.Handled = true;
// some other stuff
}
}
private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
{
if(e.KeyChar == '\r')
{
e.Handled = true;
// some other stuff
}
}
Is there a way to connect the keypress event dynamically to the focused textbox?
EDIT:
Something like:
void KeyPress(object sender, KeyPressEventArgs e)
{
foreach(Control c in this)
{
if(c == TextBox && c.Focused)
{
if(e.KeyChar == '\r')
{
// do something
}
}
}
}
You can do that :
textBox1.KeyPress += new KeyPressEventHandler(textBox_KeyPress);
textBox2.KeyPress += new KeyPressEventHandler(textBox_KeyPress);
private void textBox_KeyPress(object sender, KeyPressEventArgs e)
{
if(e.KeyChar == '\r')
{
e.Handled = true;
// some other stuff
Console.WriteLine(((TextBox)sender).Name); //actual textbox name
}
}

Speech Recognition RecognizeAsync on keydown

Im trying to have my program listen to me only when i press the shift key so called Push to talk and i tried to use the following code but when the key is pressed for longer then 1-2 seconds i get the following error:
An unhandled exception of type 'System.InvalidOperationException' occurred in System.Speech.dll
Additional information: Cannot perform this operation while the recognizer is doing recognition.
Here is part of the code:
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.ShiftKey)
{
label1.BackColor = Color.Green;
label1.Text = "Speak";
RecEngine.RecognizeAsync(RecognizeMode.Multiple);
e.SuppressKeyPress = true;
}
}
private void Form1_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.ShiftKey)
{
label1.BackColor = Color.Yellow;
label1.Text = "Ready";
RecEngine.RecognizeAsyncStop();
e.SuppressKeyPress = true;
}
}
The Form1_KeyDown function is called repetitively while the button is pressed.
You need to check whether the button has already been pressed
private bool recognitionRunning;
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.ShiftKey && !recognitionRunning)
{
label1.BackColor = Color.Green;
label1.Text = "Speak";
RecEngine.RecognizeAsync(RecognizeMode.Multiple);
e.SuppressKeyPress = true;
recognitionRunning = true;
}
}
private void Form1_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.ShiftKey)
{
label1.BackColor = Color.Yellow;
label1.Text = "Ready";
RecEngine.RecognizeAsyncStop();
e.SuppressKeyPress = true;
recognitionRunning = false;
}
}

Textbox on which Keys Delete and backspace Works

I need to have a text box on which events of Delete and Backspace works.Is it possible to have such a text box in C#,or restrict the behavior of text box in such a way. Other keys do not work.
Use TextBox.KeyPress event:
private void textBox1_KeyPress(object sender, System.Windows.Forms.KeyEventArgs e)
{
if (e.KeyCode == Keys.Delete || e.KeyCode == Keys.Back)
{
// your stuff
}
e.Handled = true;
}
For winforms you can do it like this:
protected void myTextBox_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
e.Handled = !IsValidCharacter(e.KeyChar);
}
private bool IsValidCharacter(Keys c)
{
bool isValid = false;
if (c == Keys.Space)
{
isValid = true;
}
return isValid;
}
If you want delete key works ..
private void textBox1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
Keys k = e.KeyCode
If Not (k = Keys.Back Or k = Keys.Delete)
{
e.Handled = True
}
}

Categories

Resources