I am currently using Visual Studio, and I don't know if this is a glitch or not, but when I go into the form properties, and show the events, there are two events called KeyDown and KeyUp. Now when I do the same for a PictureBox, it has way less events and no KeyDown and KeyUp events. Does the PictureBox support less events then other things? Is this a glitch?
Screenshot of Form1 properties:
Screenshot of PictureBox1 properties:
As others here have stated, the most appropriate method for capturing keyboard event in this situation is to intercept key events at the Form level, as the PictureBox control is incapable of receiving focus and lacks exposed key events.
To accomplish this, first set the KeyPreview property of the form to true within the designer, or alternatively within the form's constructor:
this.KeyPreview = true;
Then, subscribe to the KeyUp event:
this.KeyUp += MainForm_KeyUp;
Finally, use an event handler similar to as follows to intercept and process key events:
private void MainForm_KeyUp(object sender, KeyEventArgs e)
{
switch (e.KeyCode)
{
case Keys.A:
// Perform some action...
break;
case Keys.B:
// Perform some action...
break;
case Keys.End:
// Perform some action...
break;
// etc...
}
}
If you intend to "consume" the key from within the event handler, you may set the Handled property of the KeyEventArgs object as follows:
e.Handled = true;
Its not a glitch. Its the way it is. You don't type in PictureBox.
If you need to do some task through keys, route it through form only
You can use a simple trick to handle keyboard events with PictureBox.
Add an handler for MouseEnter and MouseLeave events so to set focus to the PictureBox when the cursor is over. Doing so it will catch keyboard events.
...
_myPictureBox.MouseEnter += new EventHandler(myPictureBox_MouseEnter);
_myPictureBox.MouseLeave += new EventHandler(myPictureBox_MouseLeave);
_myPictureBox.KeyDown += new KeyEventHandler(myPictureBox_event_KeyDown);
...
private void myPictureBox_MouseEnter(object sender, EventArgs e)
{
Focus();
}
private void myPictureBox_MouseLeave(object sender, EventArgs e)
{
FindForm().ActiveControl = null;
}
private void myPictureBox_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Delete)
MessageBox.Show("Bye");
}
The PictureBox is used to display images and therefore has no need for keyboard input. It does not take the focus and does not interact with the keyboard.
Event handlers for KeyDown, KeyPress and KeyUp can be added to a PictureBox. For example, in Visual Basic, for a PictureBox called MyPicBox, add a handler for MyPicBox.MouseEnter and put the line MyPicBox.Select() in the handler.
The picture box will now respond to keyboard events. For example, suppose the picture box is showing a visual display of a scientific image. Key combos like Shift-Ctrl-H or Shift-Ctrl-V can be intercepted by a PictureBox.KeyDown handler to invoke some cursor position dependent action such as displaying a cut through the underlying array:
Private Sub MyPicBox_KeyDown(sender As Object, e As KeyEventArgs) Handles MyPicBox.KeyDown
If e.Control AndAlso e.Shift Then
Select Case e.KeyCode
Case Keys.V
MyCut = Cut.VERTICAL
Exit Select
Case Keys.H
MyCut = Cut.HORIZONTAL
Exit Select
Case Keys.N
MyCut = Cut.NONE
Exit Select
End Select
End If
End Sub
The keyboard events are listed at https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.picturebox
Related
I add a function that adds text to FlowDocument when the mouse clicks.
There is no Click event in FlowDocument, so I listen to FlowDocument.MouseLeftButtonDown and MouseLeftButtonUp and check whether the mouse moves between down and up. When I click the mouse left button, the text successfully adds. However, I can't select any text in the FlowDocument.
I tried PreviewMouseLeftButtonDown and PreviewMouseLeftButtonUp. The behavior is the same. Isn't there a PostMouseLeftButtonDown?
My Code:
Point mouseDownPoint;
private void doc_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
mouseDownPoint = Mouse.GetPosition(doc);
e.Handled = true;
}
private void doc_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
var mouseUpPoint = Mouse.GetPosition(doc);
if ((mouseUpPoint - mouseDownPoint).Length < 8) /* add text */;
}
The control handles the event internally.
If you register the event handler programmatically like this, your doc_MouseLeftButtonUp event handler should get invoked (note that last handledEventsToo parameter):
doc.AddHandler(ContentElement.MouseLeftButtonUpEvent,
(MouseButtonEventHandler)doc_MouseLeftButtonUp, true);
Note that you may also have to take care of the MouseLeftButtonUp that is raised by the control itself.
I found the solution. Listen to FlowDocument.MouseLeftButtonDown and do not use e.Handled=true and listen to FlowDocumentScrollViewer.PreviewMouseLeftButtonUp will get text selection and add text behavior at the same time.
I have a button for which I set the KeyPress event.
this.myButton.KeyPress += new KeyPressEventHandler(this.myButtonEvent_keypress);
private void myButtonEvent_keypress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)Keys.Return || e.KeyChar == (char)Keys.Space)
{
// do something
}
}
Now whenever the Space key is pressed, I get the event triggered. This part works fine.
But for some reason, the Enter key press is not triggering the KeyPress
event. Also Alt, Ctrl, Shift are not working.
How can I make the button receive Enter key press?
UPDATE:
I tried below too without any luck
this.myButton.MouseClick += new MouseEventHandler(myButton_Click);
When a Button has focus and you press Enter or Space the Click event raises.
So to handle Enter or Space it's enough to handle Click event and put the logic you need there.
So you just need to use button1.Click += button1_Click;
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("Clicked!");
}
If you really want to know if Enter or Space was pressed, you can hanlde PreviewKeyDown event:
private void button1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
if(e.KeyCode== Keys.Enter)
{
MessageBox.Show("Enter");
}
if (e.KeyCode == Keys.Space)
{
MessageBox.Show("Space");
}
}
Enter and space can be handled using click event.
this.myButton.Click += new EventHandler(myButton_Click);
The Button control in WinForms will eat Enter, Space, Tab, ESC, and a few other special key's press events. One method to intercept these events is to override Control.ProcessDialogKey. Or you can override IsDialogKey to say if a key should be handled as a special case.
A another option is to set KeyPreview = true on you parent Form. Then you can handle all KeyPress events at the Form level and use Form.ActiveControl if you need to see what control has Focus
I am designing a custom calculator. Any key pressed on this calculator form should be captured at the 'Form' level. To this end, I've the following code-
private void BindControlMouseClicks(Control con)
{
con.MouseClick += delegate(object sender, MouseEventArgs e)
{
TriggerMouseClicked(sender, e);
};
// bind to controls already added
foreach (Control i in con.Controls)
{
BindControlMouseClicks(i);
}
// bind to controls added in the future
con.ControlAdded += delegate(object sender, ControlEventArgs e)
{
BindControlMouseClicks(e.Control);
};
}
private void TriggerMouseClicked(object sender, MouseEventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
this.KeyPreview = true;
this.KeyPress +=new KeyPressEventHandler(Form1_KeyPress);
BindControlMouseClicks(this);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
this.MaximizeBox = false;
}
void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
string key;
switch (e.KeyChar)
{
case '\r':
key = "ENTER";
break;
case ' ':
key = "SPACE";
break;
case (char)27:
key = "ESCAPE";
break;
default:
key = e.KeyChar.ToString();
break;
}
/* Some other code */
}
I've set breakpoints and added debug statements in the event handler function Form1_Keypress. Surprisingly all relevant keys ( Numerals, alphabets, Escape, Space etc ) hit the callback function except the ENTER key. This happens with the NUMPAD ENTER key too.
Any ideas why only the ENTER key is being handled differently?
EDIT
My form has a bunch of buttons and a textbox. When I place the focus on the textbox, voila, the callback is called for the ENTER key too! But if I put the focus on any of the buttons, no event is generated on ENTER key press. As I've set KeyPreview to true, the form must be getting this event irrespective of where the focus is, right?
EDIT 2
This happens only when there are buttons on the form and at least on of them has focus on it. If any other control has focus on it, it works just fine. I have not set AcceptButton attribute of the form.
As MSDN says(.NET TextBox - Handling the Enter Key), you can try use (char)Keys.Return instead of \r:
switch (e.KeyChar)
{
case (char)Keys.Return:
key = "ENTER";
break;
Also may be there is some button with AcceptsReturn property set to true on the form?
The problem I describe arises when any of the buttons on my form has focus. So I did these two things -
Set any_button.TabStop = false initially. This makes sure none of the buttons get focus when the form is launched
When we click a button with a mouse, it regains focus. And we end up in the same problem. To solve this, in the Mouse Click Handler, I just pass the focus from the button to some other control. For instance, my form had a menustrip and I just invoked, menustrip1.Focus() in the Mouse Click Handler.
Now, ENTER keypresses are caught in Form_Keypress handler.
This is a workaround. But I am yet to figure out why ENTER keypress is not caught by the form when the focus is on a button and not on other controls. I would be glad to have more answers for this Qn
How can I tell if a Control.Click event was triggered by the mouse or by the keyboard?
Edit:
Handling MouseClick and KeyPress does't work for me, because then how would I know if something else triggered the click? (e.g. PerformClick)
You can't. Use the Control.MouseClick event and the Control.KeyPress event so you can tell the source of the event. And remember that a space on the control with focus and a Ctrl+ key can generate a click on a button as well.
You can not tell, but you can use MouseClick and KeyPress if you need to know what originated the event.
void handler(object sender, EventArgs e)
{
bool mouseEvent = (e is MouseEventArgs);
bool keyEvent = (e is KeyEventArgs);
bool performClick = (e is EventArgs) && !keyEvent && !mouseEvent;
}
I am working on a silverlight app that you need to enter information into a textbox and then just hit enter. Well there is no onclick event, that I could find, so what I did was use the onkeypressup event and check if it was the enter key that was pressed if so do "blah".
It just feels like there is a better way to do this. So the question is, is there?
I thinks that's the way to catch Key.Enter.
Also, you're code will be more readable if you use the KeyDown event instead of the KeyUp event.
If you only care about catching Key.Enter for a single control then your approach is correct.
You can also catch the Key.Enter for a group of related controls by using the KeyDown event of their container ("Event Bubbling").
Do you really want it in the textbox? I would put a onkeyup handler on the container (e.g. Grid, Canvas) to press the button anywhere on the form.
This will work if you use want to bind the Command property instead of using the Click event. Start by creating an event handler for Click (see below) and then in the KeyUp do:
private void MyTextBox_KeyUp(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter) SomeButton_Click(this, null);
}
private void SomeButton_Click(object sender, RoutedEventArgs e)
{
ICommand cmd = SomeButton.Command;
if (cmd.CanExecute(null))
{
cmd.Execute(null);
}
}
I use the following implementation when using the command pattern:
private void MyTextBox_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
BindingExpression b = MyTextBox.GetBindingExpression(TextBox.TextProperty);
if (b != null)
b.UpdateSource();
ICommand cmd = SomeButton.Command;
if (cmd.CanExecute(null))
cmd.Execute(null);
}
}
When you press Enter, the data source of the textbox is not updated and the command uses an old value. Therefore you have to call UpdateSource before executing the command.
Of course you can catch the event on a higher level than the textbox.
Well, Im preaty new to Silverlight and I created HitEnter beahaviour for button which have one DependencyProperty Button.
And I manulay wire up Button and Behavior (in code behind) and then when enter is hit I inovke the command on the button.