Reputation: 63
I am currently in the process to implement a application-wide keyboard hook for one of my applications.
This is done by using a IMessageFilter
implementation overriding the PreFilterMessage
method and adding that IMessageFilter
to my main form.
As far as i was able to test, most of the number and letter keys work without any problem, but when it comes to, let's say, Keys.Left
(code 37), the WParam
of the Message
contains a, as it seems, wrong value (code 39).
And yes, i already did a bit-AND with Keys.KeyCode
(which is 65535 btw. which means it wouldn't even matter).
If anyone has an idea or a hint why it works for letter/number keys but not for Keys.Left
I would highly appreciate that.
Code:
public bool PreFilterMessage(ref Message m)
{
if(m.Msg == WM_KEYDOWN)
{
_keyTable[(Keys)m.WParam & Keys.KeyCode] = true;
}
if(m.Msg == WM_KEYUP)
{
_keyTable[(Keys)m.WParam & Keys.KeyCode] = false;
}
return false;
}
Upvotes: 0
Views: 557
Reputation: 11
I Faced the same problem in Numpad Keys.
I Did the following:
VB.NET:
Dim KeyPressed As String
Select Case CType(m.WParam.ToInt32(), Keys)
Case Keys.NumPad1 : KeyPressed = "1"
Case Keys.NumPad2 : KeyPressed = "2"
Case Keys.NumPad3 : KeyPressed = "3"
Case Keys.NumPad4 : KeyPressed = "4"
Case Keys.NumPad5 : KeyPressed = "5"
Case Keys.NumPad6 : KeyPressed = "6"
Case Keys.NumPad7 : KeyPressed = "7"
Case Keys.NumPad8 : KeyPressed = "8"
Case Keys.NumPad9 : KeyPressed = "9"
Case Keys.NumPad0 : KeyPressed = "0"
Case Else : KeyPressed = Microsoft.VisualBasic.ChrW(m.WParam)
End Select
Upvotes: 1