I want to capture a Key Press and get the e.Key
Enum int
value.
This captures and displays the key that was pressed.
XAML
<ToggleButton x:Name="btnDPad_L"
Content="{Binding DPad_L_Text}"
HorizontalAlignment="Left"
VerticalAlignment="Top"
Margin="80,172,0,0"
Height="30"
Width="30"
PreviewKeyDown="btnDPad_L_PreviewKeyDown" />
C#
private void btnDPad_L_PreviewKeyDown(object sender, System.Windows.Input.KeyEventArgs e)
{
int keyEnum = (int)e.Key; // Capture
MessageBox.Show(e.Key.ToString()); // Letter
MessageBox.Show(keyEnum.ToString()); // Enum
}
Problem
This gives the System.Windows.Input
Micrcorosft Keys Enum int values.
System.Windows.Input.Keys Enum
W = 66
A = 44
S = 62
D = 47
I need the System.Windows.Forms
KeyEventArgs
different int values.
System.Windows.Forms.Keys Enum
W = 87
A = 65
S = 83
D = 68
Errors
When I try to use System.Windows.Forms.KeyEventArgs
I get the errors:
No overload for 'btnDPad_L_PreviewKeyDown' matches delegate 'KeyEventHandler'
I try to attach a handler to Window_Loaded
event:
btnDPad_L.KeyDown += new System.Windows.Forms.KeyEventArgs(btnDPad_L_PreviewKeyDown);
cannot convert from 'method group' to 'Keys'