Esc handler ignored - c#

Following code, doesn't get triggered when Esc is pressed. Can somebody give me insight?
I like to change the cursor (let's say from drawing mode turn it into pointer mode)
public override void OnKeyDown(MyCanvas dc, KeyEventArgs e)
{
if (e.Key == Key.Escape)
{
_line = null;
e.Handled = true;
}
}
Thanks.

#amit's solution:
public override void OnKeyDown(MyCanvas dc, KeyEventArgs e)
{
if (e.Key == Key.Escape)
{
_line = null;
dc.CurrentTool = ToolType.Pointer;
dc.Cursor = Cursors.Arrow;
dc.ReleaseMouseCapture();
}
}

Related

Using Double Right Click in C#

I am fairly new with C# and I want to know if there is a way to implement a double right click in this event handler? Can anyone tell me how? thanks
private void pictureBox_MouseClick(object sender, MouseEventArgs e){
if(e.Button == MouseButtons.Left)
{
MessageBox.Show("A");
}
else if(e.Button = MouseButtons.Right)
{
MessageBox.Show("B");
}
else if(e.Button = MouseDoubleClick.Right) <--how to fix this?
{
MessageBox.Show("C");
}
else
{
MessageBox.Show("D");
}
}
You can use the ClickCount propery of MouseRightButtonDown event.
private void OnMouseDownClickCount(object sender, MouseButtonEventArgs e)
{
if (e.ClickCount == 1)
{
// one click.
}
if (e.ClickCount == 2)
{
// two clicks.
}
}
Override the WndProc function and listen for WM_RBUTTONDBLCLK, which as can be seen on this pinvoke page is {0x0206}.
Then
public class RButton : Button
{
public delegate void MouseDoubleRightClick(object sender, MouseEventArgs e);
public event MouseDoubleRightClick DoubleRightClick;
protected override void WndProc(ref Message m)
{
const Int32 WM_RBUTTONDBLCLK = 0x0206;
if (m.Msg == WM_RBUTTONDBLCLK)
DoubleRightClick(this, null);
base.WndProc(ref m);
}
}

C# Windows Forms KeyDown event fires continiously [duplicate]

This question already has answers here:
how not to allow multiple keystokes received at one key press?
(3 answers)
Closed 6 years ago.
In a Windows forms application I'm trying to execute some function when I press a hotkey using Form's KeyDown event. The problem is that when I keep the hotkey pressed the event fires continuously.
I just want to execute some function at the first time I press the button and another function when I release it.
Here is the code I used to accomplish this operation:
bool isPressed_Num7 = false;
bool isPressed_Num9 = false;
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyData == Keys.NumPad7 && !isPressed_Num7)
{
isPressed_Num7 = true;
Console.WriteLine("Keydown 7");
}
if (e.KeyData == Keys.NumPad9 && isPressed_Num9)
{
isPressed_Num9 = true;
Console.WriteLine("Keydown 9");
}
}
private void Form1_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyData == Keys.NumPad7)
{
isPressed_Num7 = false;
Console.WriteLine("Keyup 7");
}
if (e.KeyData == Keys.NumPad9)
{
isPressed_Num9 = false;
Console.WriteLine("Keyup 9");
}
}
I'm going to use a bunch of hotkeys. So what I'm asking is there a better way to accomplish what I'm trying to do?
You could just a dictionary instead of one variable per key.
Dictionary<Keys, bool> keysPressed = new Dictionary<Keys, bool>();
private void Form1_KeyUp(object sender, KeyEventArgs e)
{
keysPressed[e.KeyData] = false;
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (!keysPressed.ContainsKey(e.KeyData) || !keysPressed[e.KeyData]) {
Console.WriteLine(e.KeyData);
keysPressed[e.KeyData] = true;
}
}
Try this:
bool isPressed_Num7 = false;
bool isPressed_Num9 = false;
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if ((e.KeyData == Keys.NumPad7) && (!isPressed_Num7))
{
isPressed_Num7 = true;
Console.WriteLine("Keydown 7");
}
if ((e.KeyData == Keys.NumPad9) && (!isPressed_Num9))
{
isPressed_Num9 = true;
Console.WriteLine("Keydown 9");
}
}
private void Form1_KeyUp(object sender, KeyEventArgs e)
{
if ((e.KeyData == Keys.NumPad7)&&(isPressed_Num7==true))
{
isPressed_Num7 = false;
Console.WriteLine("Keyup 7");
}
if ((e.KeyData == Keys.NumPad9)&&(isPressed_Num9==true))
{
isPressed_Num9 = false;
Console.WriteLine("Keyup 9");
}
}
This solution uses the recommended way to handle keyboard shortcuts according to Microsoft. And allows you to assign action to invoke when pressing a key combination.
public partial class Form1 : Form
{
private readonly IDictionary<Keys, Action> _operationsMap = new Dictionary<Keys, Action>();
public Form1()
{
InitializeComponent();
RegisterKeyShortcuts();
}
private void RegisterKeyShortcuts()
{
_operationsMap.Add(Keys.Control | Keys.F, WhenPressingF);
}
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (_operationsMap.ContainsKey(keyData))
{
_operationsMap[keyData].Invoke();
return true;
}
return base.ProcessCmdKey(ref msg, keyData);
}
protected void WhenPressingF()
{
MessageBox.Show("What the Ctrl+F?");
}
}

How do i set the label and display an integer value from a TextBox to the label when the enter key is pressed in wpf c#?

Here's the code. I have modified it bit?
public partial class MainWindow : Window
{
int coursenumber;
public MainWindow()
{
InitializeComponent();
}
private void textBox_TextChanged(object sender, TextChangedEventArgs e)
{
bool res = int.TryParse(textBox.Text, out coursenumber);
if (res == true)
{
//success
}
}
private void textBox_KeyDown(object sender, KeyEventArgs e)
{
//Check if the key that was pressed was the Enter key.
if (e.Key == Key.Enter)
{
//logic goes here
}
}
Register to the Key Down event and check
private void textBox_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Return)
{
// your logic here
}
}
What you're looking for is the KeyDown event. Subscribe to this event on your TextBox like this:
<TextBox KeyDown="textBox_KeyDown" ... />
And your event handler will look something like this:
private void textBox_KeyDown(object sender, KeyEventArgs e)
{
//Check if the key that was pressed was the Enter key.
if (e.Key == Key.Enter)
{
bool res = int.TryParse ...
//The rest of your logic here.
}
}
Thanks everyone, i was able to get it working from your responses. here's the modification
public partial class MainWindow : Window
{
int coursenumber;
public MainWindow()
{
InitializeComponent();
}
private void textBox_TextChanged(object sender, TextChangedEventArgs e)
{
bool res = int.TryParse(textBox.Text, out coursenumber);
if (res == true)
{
//success
}
}
private void textBox_KeyDown(object sender, KeyEventArgs e)
{
//Check if the key that was pressed was the Enter key.
if (e.Key == Key.Enter)
{
label2.Content = coursenumber;
}
}

Axwindows media player dectect keydown in full screen mode

I have ax windows media player in my windows forms application. When the user double clicks on it, it becomes full screen.
PROBLEM: I want the user to be able to go back to normal screen when he presses the "escape key".
I have put a keydown event on the ax media player. This key down event works when in normal mode, but fails when the media player is made full screen.
WMPLarge.KeyDownEvent += new AxWMPLib._WMPOCXEvents_KeyDownEventHandler(Form1_KeyDown);
private void Form1_KeyDown(object sender, AxWMPLib._WMPOCXEvents_KeyDownEvent e)
{
if (e.nKeyCode == 27)
{
MessageBox.Show("");
WMPLarge.fullScreen = false;
WMPSmall.fullScreen = false;
}
}
How can I achieve this ?
Here is one code snippet I used, I hope that helps.
public partial class Form16 : Form,IMessageFilter
{
public Form16()
{
InitializeComponent();
}
private void Form16_Load(object sender, EventArgs e)
{
this.axWindowsMediaPlayer1.URL = #"D:\MyVideo\myfile.wmv";
Application.AddMessageFilter(this);
}
private void Form16_FormClosing(object sender, FormClosingEventArgs e)
{
Application.RemoveMessageFilter(this);
}
#region IMessageFilter Members
private const UInt32 WM_KEYDOWN = 0x0100;
public bool PreFilterMessage(ref Message m)
{
if (m.Msg == WM_KEYDOWN)
{
Keys keyCode = (Keys)(int)m.WParam & Keys.KeyCode;
if (keyCode == Keys.Escape)
{
this.axWindowsMediaPlayer1.fullScreen = false;
}
return true;
}
return false;
}
#endregion
}

Combo Box Enter Event WPF

I need to have an event fired whenever Enter is pressed inside of the combobox. This is a WPF C# 4.0 control, and I am unable to find a specific event handler to do this. I think I am missing something, as this seems like something that would be included. Is there preexisting code to accomplish this task?
I have also tried:
private void comboBox1_SelectionChanged(
object sender,
SelectionChangedEventArgs e)
{
if (e.Equals(Key.Enter))
{
// Do Something
}
}
private void comboBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Return)
{
// do stuff
}
else
{
// do stuff
}
}
private void comboBox1_KeyUp(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
// do stuff
}
}
or
private void comboBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
// do stuff
}
}
The difference being that KeyUp is when the key is released, KeyDown is when it is first pressed.

Categories