Can I determine which keyboard a key pressed is on? - wpf

Users of my application have a second keyboard with special function keys. Unfortunately, the keys are mapped to buttons such as F, G, F1 and so on. I would like to handle PreviewKeyDown and prevent any keys from these keyboards having an effect in normal controls such as TextBoxes.
In WPF, is there any way of determining which keyboard raised the event?

No, it is not possible directly in WPF.

using System.Windows.Input you could be able to achieve this by capturing the event that is fired in your code behind. Sample code below shows how this can be done in Textbox.
private void SampleTextbox_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Delete) // delete key is pressed
{
e.Handled = true; // Ignore key press
}
}

Related

The KeyDown event of a Simple style combo box is raised two times

I have simple win form application with the tab and combo box controls.
Combo box control has a style of "Simple".
Tab control has key down event.
When I press Enter key on the combo control it fires TWO key down events. If you change the combo style to any other, the key down event fires only one which it is something I expect.
Has anybody got any ideas why I am getting two key down events for single enter key press?
I have found similar issue on the Microsoft website, but that was related to .NET 1.0.
http://support.microsoft.com/kb/814970
It has probably something to do with the Enter key having pre-defined behavior for the Simple DropDown style.
You can try this work-around in the KeyDown event:
void comboBox1_KeyDown(object sender, KeyEventArgs e) {
e.SuppressKeyPress = true;
// do stuff
}
As you can guess, the KeyPress event won't fire now.
If you need to still process things in the KeyPress event, you can try this work-around:
void comboBox1_KeyPress(object sender, KeyPressEventArgs e) {
if (e.KeyChar == (char)Keys.Enter) {
e.Handled = true;
} else {
// do stuff
}
}
KeyPress from Combobox accepts just 'char' keys. For you purpose, please, use KeyDown event for combobox and e.Handled property.
Then your code will work and look like:
private void comboBox2_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
comboBox2.KeyDown += new KeyEventHandler(comboBox2_SelectionChangeCommitted);
}
if (e.KeyCode != Keys.Enter)
{
e.Handled = false;
}
}

Detect “tab” key press from the last cell/row of a DataGrid

In a WPF Datagrid, how to detect when the user press the key "Tab" from the last cell/row ? With KEY_DOWN event the selected cell is unknown, with FOCUS_OUT we don't know the key pressed.
private void dataGrid1_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Tab)
{
MessageBox.Show("now tab!!!");//Here u know the tab press
MessageBox.Show(dataGrid1.SelectedIndex.ToString());//Here u know the cell/row!
}
}
You want to handle the PreviewKeyDown event on the DataGrid itself. Within the handler you can then check to see which cell is currently selected.

Prevent Silverlight DataGrid from Processing Enter Key

I have a datagrid in a Silverlight application. Clicking on the first row, and then pressing the Enter key causes the selection to move to the next row. I do not want this behavior - the Enter key is used for totally different purposes on this screen.
I realize that this is part of the editing framework, but I need a way to turn it off. I tried setting IsReadOnly to True (even though the control isn't technically read-only) and that didn't have any effect.
I attached to the datagrid KeyDown event but it's not called when the Enter key is pressed. It works fine for other keys.
I'm stumped. Thanks for your help.
Yeah, that's annoying.
As far as I know, the only option is to create your own control that inherits from DataGrid, and do what is needed before passing the event on to the base.
public class NewDataGrid : DataGrid
{
protected override void OnKeyDown(KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
//do what is needed here
e.Handled = true;
}
base.OnKeyDown(e);
}
}

Select ListBox item only on Click or Enter key in Silverlight

When I populate a ListBox with RIA Services, an item is automatically selected. This triggers the SelectionChanged event. If I move the selection up or down with the arrow keys, the event also gets triggered.
I don't want this. I want the user to press enter or click the item for it to be selected. How do I accomplish this?
You could handle the MouseLeftButtonDown and KeyDown events for the ListBox. For the KeyDown event, you'll need to check the EventArgs to determine whether the Enter key was pressed (as opposed to any other key).
These events can fire even when an item is not selected (e.g., if the user clicks inside the ListBox but not over an actual item), so within your event handlers you should check for this.
Your event handlers might look something like this:
public void MyListBox_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
ItemSelected();
}
public void MyListBox_KeyDown(object sender, KeyEventArgs e)
{
if ((e.Key & Key.Enter) == Key.Enter)
{
ItemSelected();
}
}
public void ItemSelected()
{
if (MyListBox.SelectedItem != null)
{
// Handle item selection here
}
}
These are off the top of my head, so you may need to tweak these slightly to get them to work exactly right. Hopefully you see the general idea though.
Another way to do it would be to simply remove the SelectionChanged event handler when populating the ListBox with items (use the "-=" syntax), then re-attach it once this operation is complete.
I'd recommend doing it this way (since you're concerned about the event firing when the list is populated). It wouldn't stop the users from selecting items using the Up and Down arrow keys, but unless you have a really good reason for doing so you're making things unnecessary inconvenient (users don't want to be arbitrarily restricted from doing things that ought to work).

How to tell if Shift is being pressed in a MouseDown event?

I am catching a MouseDown event on a control, which gives me a MouseEventArgs object in the signature. Now I want to be able to tell if the user was holding down the "Shift" or "Control" key when they clicked. But the MouseEventArgs object doesn't contain any keyboard information!
What's the easiest way of telling whether the keyboard Shift/Ctrl keys were being held at the time of the click?
Use the Control.ModifierKeys property to see what's pressed. For example:
private void Form1_MouseClick(object sender, MouseEventArgs e) {
if (Control.ModifierKeys == Keys.Control) {
Console.WriteLine("Ctrl+Click");
}
}
Other modifiers are Keys.Alt and Keys.Shift. Find combinations with, say, (Keys.Control | Keys.Shift).
In C#, you can check using -
Keyboard.IsKeyDown(Key.LeftShift) or key.RightShift
http://msdn.microsoft.com/en-us/library/system.windows.input.keyboard.iskeydown.aspx
http://msdn.microsoft.com/en-us/library/system.windows.input.key.aspx

Resources