Skip to content

Commit

Permalink
use SendInput instead of keybd_event
Browse files Browse the repository at this point in the history
* keybd_event function has been superseded refers to https://msdn.microsoft.com/library/windows/desktop/ms646304(v=vs.85).aspx
* can change status of Caps Lock and Scroll Lock now
  • Loading branch information
myfreeer authored May 13, 2017
1 parent 85cca2f commit 765b09d
Showing 1 changed file with 41 additions and 17 deletions.
58 changes: 41 additions & 17 deletions numlock.c
Original file line number Diff line number Diff line change
@@ -1,30 +1,54 @@
#include <windows.h>
#include <string.h>
#include<ctype.h>

void SetNumLock(BOOL bState) {
void SetKey(BOOL bState, unsigned int keyCode) {
BYTE keyState[256];
INPUT input[2];

GetKeyboardState((LPBYTE) & keyState);
if ((bState && !(keyState[VK_NUMLOCK] & 1)) ||
(!bState && (keyState[VK_NUMLOCK] & 1))) {
// Simulate a key press
keybd_event(VK_NUMLOCK,
0x45,
KEYEVENTF_EXTENDEDKEY | 0,
0);

// Simulate a key release
keybd_event(VK_NUMLOCK,
0x45,
KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP,
0);
if ((bState && !(keyState[keyCode] & 1)) ||
(!bState && (keyState[keyCode] & 1))) {
ZeroMemory(input, sizeof(input));
input[0].type = input[1].type = INPUT_KEYBOARD;
input[0].ki.wVk = input[1].ki.wVk = keyCode;
input[1].ki.dwFlags = KEYEVENTF_KEYUP;

// Simulate a key input
SendInput(2, input, sizeof(INPUT));
}
}

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) {
//main entry
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) {
unsigned int keyCode;

//get self path
char selfName[MAX_PATH];
GetModuleFileName(NULL, selfName, MAX_PATH);

//get file name from path
const char * ptr = strrchr(selfName, '\\');
if (ptr != NULL)
strcpy(selfName, ptr + 1);

//convert file name to lower case
for (unsigned int i = 0; i < strlen(selfName); i++)
selfName[i] = tolower(selfName[i]);
//MessageBox(NULL, selfName, NULL, MB_OK|MB_ICONINFORMATION);

if (strstr(selfName, "num"))
keyCode = VK_NUMLOCK;
if (strstr(selfName, "caps") || strstr(selfName, "capital"))
keyCode = VK_CAPITAL;
if (strstr(selfName, "scro") || strstr(selfName, "scrl"))
keyCode = VK_SCROLL;

if (!keyCode) return 1;
if (lstrlen(lpCmdLine) > 0) {
SetNumLock(FALSE);
SetKey(FALSE, keyCode);
} else {
SetNumLock(TRUE);
SetKey(TRUE, keyCode);
}
return 0;
}

0 comments on commit 765b09d

Please sign in to comment.