Skip to content

Commit

Permalink
Added ScanCode in KeyEventArgsExt
Browse files Browse the repository at this point in the history
  • Loading branch information
shadowlif authored and shadowlif committed Jul 27, 2017
1 parent e24b30c commit 33b2159
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions MouseKeyHook/KeyEventArgsExt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,21 @@ public KeyEventArgsExt(Keys keyData)
{
}

internal KeyEventArgsExt(Keys keyData, int timestamp, bool isKeyDown, bool isKeyUp, bool isExtendedKey)
internal KeyEventArgsExt(Keys keyData, int scanCode, int timestamp, bool isKeyDown, bool isKeyUp, bool isExtendedKey)
: this(keyData)
{
ScanCode = scanCode;
Timestamp = timestamp;
IsKeyDown = isKeyDown;
IsKeyUp = isKeyUp;
IsExtendedKey = isExtendedKey;
}

/// <summary>
/// The hardware scan code.
/// </summary>
public int ScanCode { get; private set; }

/// <summary>
/// The system tick count of when the event occurred.
/// </summary>
Expand All @@ -49,7 +55,6 @@ internal KeyEventArgsExt(Keys keyData, int timestamp, bool isKeyDown, bool isKey
/// </summary>
public bool IsKeyUp { get; private set; }


/// <summary>
/// True if event signals, that the key is an extended key
/// </summary>
Expand Down Expand Up @@ -77,12 +82,14 @@ internal static KeyEventArgsExt FromRawDataApp(CallbackData data)
//bit 24 Specifies the extended key state. The value is 1 if the key is an extended key, otherwise the value is 0.
bool isExtendedKey = (flags & maskExtendedKey) > 0;


Keys keyData = AppendModifierStates((Keys) wParam);
var scanCode = (int)(((flags & 0x10000) | (flags & 0x20000) | (flags & 0x40000) | (flags & 0x80000) | (flags & 0x100000) | (flags & 0x200000) | (flags & 0x400000) | (flags & 0x800000)) >> 16);

bool isKeyDown = !isKeyReleased;
bool isKeyUp = wasKeyDown && isKeyReleased;

return new KeyEventArgsExt(keyData, timestamp, isKeyDown, isKeyUp, isExtendedKey);
return new KeyEventArgsExt(keyData, scanCode, timestamp, isKeyDown, isKeyUp, isExtendedKey);
}

internal static KeyEventArgsExt FromRawDataGlobal(CallbackData data)
Expand All @@ -101,7 +108,7 @@ internal static KeyEventArgsExt FromRawDataGlobal(CallbackData data)
const uint maskExtendedKey = 0x1;
bool isExtendedKey = (keyboardHookStruct.Flags & maskExtendedKey) > 0;

return new KeyEventArgsExt(keyData, keyboardHookStruct.Time, isKeyDown, isKeyUp, isExtendedKey);
return new KeyEventArgsExt(keyData, keyboardHookStruct.ScanCode, keyboardHookStruct.Time, isKeyDown, isKeyUp, isExtendedKey);
}

// # It is not possible to distinguish Keys.LControlKey and Keys.RControlKey when they are modifiers
Expand Down

0 comments on commit 33b2159

Please sign in to comment.