OxyPlot's PlotModel and PlotController components contains their own Mouse Handlers, to which you can add or remove you own Handlers. For Instance:
this.Model.MouseMove += this.ModelOnMouseMove;
public void ModelOnMouseMove(object sender, OxyMouseDownEventArgs e)
{
// Do some mouse stuff...
}
Is there a way for the Model to capture KeyBoard Key presses in a similiar manner?
OxyPlot apparently does have a Model.Keydown Even Handler, that triggers when the PlotModel is in focus. There just doesn't appear to be much documentation on it:
this.Model.KeyDown += ModelOnKeyDown;
private void ModelOnKeyDown(object sender, OxyKeyEventArgs e)
{
if (e.Key == OxyKey.Up)
{
// Do Some Stuff
}
else if (e.Key == OxyKey.Down)
{
// Do Some Stuff
}
}
Related
I want to handle crtl+click event in WPF XAML code behind. I registered KeyDown event and MouseDown event, but I am not able to register for them together. What is that I am missing.
private void Window_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key.Equals(Key.LeftCtrl))
{
//do something
}
}
private void Window_MouseDown(object sender, MouseButtonEventArgs e)
{
if (e.ClickCount == 1)
{
//do something
}
}
Thanks,
RDV
Don't bother tracking Key Up/Down events--they're not reliable (e.g., user may switch windows between pressing and releasing). Just check Keyboard.Modifiers when you process the click.
If you only want to handle Ctrl+Click and not, for example, Ctrl+Shift+Click, then check Modifiers for an exact match:
if (e.ClickCount == 1 && Keyboard.Modifiers == ModifierKeys.Control) {
}
Otherwise, do a selective bitwise match:
if (e.ClickCount == 1 && (Keyboard.Modifiers & ModifierKeys.Control) != 0) {
}
Keyboard.Modifiers should generally be consistent: it won't reflect the release of the control key until after you process the mouse event, because all input events are processed in FIFO order.
Be sure to read up on bubbling versus tunneling events (e.g., MouseDown vs. PreviewMouseDown), and how events stop routing once they are marked as Handled.
As an alternative, you may be able to use a simple MouseGesture with a MouseAction of LeftClick and Modifiers of Control. It really depends on what your UI looks like, and whether you want a chance at handling the event before any descendant controls. Using a mouse gesture should work in (mostly) the same scenarios as handling the bubbling MouseDown event. But if you need to rely on tunneling events like PreviewMouseDown, then a gesture wouldn't work.
Do something like this, with a flag:
bool _isControlPressed = false;
private void Window_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key.Equals(Key.LeftCtrl))
{
_isControlPressed = true;
}
}
private void Window_KeyUp(object sender, KeyEventArgs e)
{
if (e.Key.Equals(Key.LeftCtrl))
{
_isControlPressed = false;
}
}
private void Window_MouseDown(object sender, MouseButtonEventArgs e)
{
if (e.ClickCount == 1 && _isControlPressed)
{
//do something
}
}
The _isControlPressed flag is set/unset in the key down/key up events. You can then use this flag in other methods to determine the key state.
private void MeretOK_Click(object sender, EventArgs e)
{
//code
}
private void MeretTB_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode==Keys.Enter)
{
MeretOK_Click();
}
}
How can I start an event with a hotkey?
(I know I can just copy the code there but that is ugly)
If I just copy the code and run it I hear a beep. Why?
First, you have to allow your form to handle key events globally (set the form KeyPreview property to true) and to mark your event as internally handled. Second, if the purpose of this code is to simulate the mouse click on a specific control (programmatic click), for example a button, there is an easier way to accomplish it:
private void MeretTB_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
e.Handled = true;
myButton.PerformClick();
}
}
I'm working on a program that allows the user to enter a barcode via scanner and then do stuff, and I've got most of it worked out, I just can't figure out which action method for textBox1 would allow me to do something when "Enter" was hit while in the textBox. I've looked at the description of most of the actions, and I can't find one that sounds like it would work.
Is there one that would work? Or do I just have to check every time a key is pressed?
You want the KeyDown / OnKeyDown or KeyUp/OnKeyUp event, just filter for the right key:
protected override void OnKeyDown(KeyEventArgs e)
{
base.OnKeyDown(e);
if (e.KeyCode == Keys.Enter)
{
// Do Something
}
}
Or in your case since your parent form is most likely subscribing to the TextBox event then you would add a method like the following using the designer:
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
// Do Something
}
}
Keep in mind that what you are calling "Action Methods" are called Events.
Try this, using the KeyUp event:
private void textBox1_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
DoSomething();
}
}
try handler the keypress event.
stop the handler and work better.
using System;
using System.Windows.Forms;
public class Form1: Form
{
public Form1()
{
// Create a TextBox control.
TextBox tb = new TextBox();
this.Controls.Add(tb);
tb.KeyPress += new KeyPressEventHandler(keypressed);
}
private void keypressed(Object o, KeyPressEventArgs e)
{
// The keypressed method uses the KeyChar property to check
// whether the ENTER key is pressed.
// If the ENTER key is pressed, the Handled property is set to true,
// to indicate the event is handled.
if (e.KeyChar != (char)Keys.Enter)
{
e.Handled = true;
}
}
public static void Main()
{
Application.Run(new Form1());
}
}
I have added a keyPress event on a ListView. With a breakpoint on my event, I can see that most of the keys trigger the event. However, a few among which, the one I'm interested in (delete), just won't trigger my event.
Is that weird ? And no, there's no broken keys on my keyboard :D
private void listView1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)Keys.Delete)
{
ListView target = (ListView)sender;
if (target.SelectedIndices != null && target.SelectedIndices.Count > 0)
{
string ric = target.SelectedItems[0].SubItems[0].Text;
//target.Items.RemoveAt(target.SelectedIndices[0]);
ListModels.getInstance().getModel("Vols").removeRic(ric);
}
}
}
The reason for this is that the KeyPress event sends a character to the control based upon the character-key you press. However, as you'd expect, the delete key does not represent a character and is thus a non-character key.
Therefore using the KeyPress event will do nothing as you have noticed. You should use the KeyDown or KeyUp Events, either of which will work absolutely fine. The nuance being whether you want your event to fire upon pressing, or letting go of a key.
You'll want to use the KeyDown event for this.
In KeyDown use the condition as follows,
if (e.KeyCode == Keys.Delete)
{
// Your Logic....
}
Use keyDown instead; keyPress is something like a full keyDown + keyUp
The problem is that if you set EditMode property to EditOnEnter it won't fire. If you use EditOnKeyStrokeOfF2 it will fire the event
If you are looking for a solution where the user should only be able to choose from the defined items, then I believe you can do it with this:
private void DropDownRank_KeyDown(object sender, KeyEventArgs e)
{
e.SuppressKeyPress = true;
}
See this code:
private void Form1_Load(object sender, EventArgs e)
{
listView1.KeyUp += new KeyEventHandler(ListView_KeyUp);
}
/// <summary>鍵盤觸發 ListView 清單</summary>
private void ListView_KeyUp(object sender, KeyEventArgs e)
{
ListView ListViewControl = sender as ListView;
if (e.KeyCode == Keys.Delete)
{
foreach (ListViewItem eachItem in ListViewControl.SelectedItems)
{
ListViewControl.Items.Remove(eachItem);
}
}
}
I tried all the stuff mentioned above but nothing worked for me, so im posting what i actually did and worked, in the hopes of helping others with the same problem as me:
Add an event handler in the constructor:
public partial class Test
{
public Test()
{
this.RemoveHandler(KeyDownEvent, new KeyEventHandler(Test_KeyDown));
// im not sure if the above line is needed (or if the GC takes care of it
// anyway) , im adding it just to be safe
this.AddHandler(KeyDownEvent, new KeyEventHandler(Test_KeyDown), true);
InitializeComponent();
}
//....
private void Test_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Delete)
{
//your logic
}
}
}
How to catch keyboard events of the WinForm main form, where other controls are.
So I want to catch one event Ctrl + S and doesn't matter where focus is.
But without Pinvoke (hooks and such ...)
Only .NET managed internal power.
Try this code. Use the interface IMessageFilter you can filter any ctrl+key.
public partial class Form1 :
Form,
IMessageFilter
{
public Form1()
{
InitializeComponent();
Application.AddMessageFilter(this);
this.FormClosed += new FormClosedEventHandler(this.Form1_FormClosed);
}
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
Application.RemoveMessageFilter(this);
}
public bool PreFilterMessage(ref Message m)
{
//here you can specify which key you need to filter
if (m.Msg == 0x0100 && (Keys)m.WParam.ToInt32() == Keys.S &&
ModifierKeys == Keys.Control)
{
return true;
}
else
{
return false;
}
}
}
I tested this and worked for me.
the Form Class (System.Windows.Forms) has OnKeyDown, OnKeyPress, and OnKeyUp event methods that you can use to detect Ctrl + S
use the KeyEventArgs in those methods to determine which keys were pressed
EDIT
be sure to enable Form.KeyPreview = true; so the form will capture the events regardless of focus.
Handle the KeyDown on the form and all its controls.
private void OnFormLoad(object sender, EventArgs e)
{
this.KeyDown += OnKeyDown;
foreach (Control control in this.Controls)
{
control.KeyDown += OnKeyDown;
}
}
private void OnKeyDown(object sender, KeyEventArgs e)
{
if (e.Control)
{
if (e.KeyValue == (int)Keys.S)
{
Console.WriteLine("ctrl + s");
}
}
}
You may add a MenuStrip and then create a menu strip item named save and give it a short cut Ctrl + S. Add a event handler for that. This will fire even if the focus is on other control on the form. If you don't like to see the MenuStrip; you can set visible = false too. I must admit this is ugly.