A tool for get Virtual-Key Code of the pressed key

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
neorobin
Posts: 47
Joined: 01 May 2012 12:18

A tool for get Virtual-Key Code of the pressed key

#1 Post by neorobin » 06 Sep 2016 22:35

Code: Select all

@echo off
setlocal enabledelayedexpansion

call :make "%~f0"

for /L %%a in () do (

    neoGetKey.exe 500 x

    set "err=!errorlevel!"
    if !err! equ 255 (echo;   time out) else echo;   pressed key value is : !err!
)

exit


:make
for /f "delims=:" %%a in ('findstr /n /i "^:BEGIN:getKEY.B64" "%~1"') do set "sk=%%a"

> getKEY.B64 cd.
     for /f "useback skip=%sk% delims=" %%f in ("%~1") do (
        if /i not "%%f"==":END:getKEY.B64" (>> getKEY.B64 echo;%%f) else (

            certutil -f -decode getKEY.B64 neoGetKey.exe
            exit /b
        )
    )
)

exit /b



neoGetKey.exe

aa77dd@163.com @ 20160907_122038

获取当前被按下的 按键 值, 支持全键盘按键, 以及鼠标 左 右 中键

参数说明:
    neoGetKey.exe timeout showMethod

    timeout         超时数, 单位 ms, 在运行超过此时间后自动退出, 并返回 255
                    如果 此项是 0  将一直等到检测到有键按下, 程序才会结束并返回键值.
                    如果 此项不存在, 最长将等到默认值 1000ms 后退出

    showMethod      返回值的显示方式, 此项为 D 以十进制显示, 此项为 X 以十六进制显示
                    如果此项不存在, 将不会显示返回值



::BEGIN:neoGetKey.C
#include <Windows.h>
#include <stdio.h>

int main(int argc, char** argv) {
    int i, t = 1000;
    char show = '\0';

    if (argc >= 2) t = atoi(argv[1]);

    if (argc >= 3) {
        show = (**(argv + 2) == 'x' || **(argv + 2) == 'X') ? 'x' : (**(argv + 2) == 'd' || **(argv + 2) == 'D') ? 'd' : '\0';
    }

    for (i = 0; t == 0 || i < t; i++) {
        int x;
        for (x = 1; x <= 254; x++)
            if (GetAsyncKeyState(x) & 0x8000) {
                if (show == 'x') printf("%x", x);
                if (show == 'd') printf("%d", x);
                return x;
            }
        sleep(1);
    }
    if (show ==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the pressed key

#2 Post by aGerman » 12 Sep 2016 15:40

neorobin

Some suggestions
- 255 is a valid key code (https://msdn.microsoft.com/en-us/library/windows/desktop/dd375731). You should rather return 0 or some value greater than 255 as default.
- GetAsyncKeyState might have been easy to use but it is a global function that determines the key pressed even if the console window doesn't have the keyboard focus. Better use ReadConsoleInput (if you want to discard the input from stdin) or PeekConsoleInput (if you want to leave the input in stdin).
- Avoid ato... functions. You can't even differentiate 0 from an invalid argument. Better use strto... functions.
- You could use a thread function together with WaitForSingleObject in order to have a more accurate time out.

C / Windows API

Code: Select all

#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <process.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>

typedef struct tagTHREADARG
{
  CRITICAL_SECTION mtx;
  HANDLE hStdin;
  INPUT_RECORD *inRec;
  int virtKeyCode;
} THREADARG;

unsigned __stdcall WaitForKeyEvent(void *ptr)
{
  THREADARG *pArg = (THREADARG*)ptr;
  DWORD NumEvents = 0, NumEventsRead = 0;
  BOOL success = FALSE;

  for (;;)
  {
    EnterCriticalSection(&pArg->mtx);
    GetNumberOfConsoleInputEvents(pArg->hStdin, &NumEvents);
    pArg->inRec = (INPUT_RECORD*)malloc(sizeof(INPUT_RECORD) * NumEvents);
    LeaveCriticalSection(&pArg->mtx);
    if (pArg->inRec == NULL)
      _endthreadex(0);

    EnterCriticalSection(&pArg->mtx);
    success = PeekConsoleInputA(pArg->hStdin, pArg->inRec, NumEvents, &NumEventsRead);
    LeaveCriticalSection(&pArg->mtx);

    if (success)
    {
      for(DWORD i = 0; i < NumEventsRead; ++i)
      {
        EnterCriticalSection(&pArg->mtx);
        if (pArg->inRec[i].EventType == KEY_EVENT && pArg->inRec[i].Event.KeyEvent.bKeyDown)
        {
          pArg->virtKeyCode = pArg->inRec[i].Event.KeyEvent.wVirtualKeyCode;
          free(pArg->inRec);
          pArg->inRec = NULL;
          LeaveCriticalSection(&pArg->mtx);
          _endthreadex(0);
        }
        LeaveCriticalSection(&pArg->mtx);
      }
    }
    EnterCriticalSection(&pArg->mtx);
    free(pArg->inRec);
    pArg->inRec = NULL;
    LeaveCriticalSection(&pArg->mtx);
  }
  return 0;
}

int main(int argc, char *argv[])
{
  THREADARG threadarg = {0};
  if ((threadarg.hStdin = GetStdHandle(STD_INPUT_HANDLE)) == INVALID_HANDLE_VALUE)
    return 256;

  DWORD t = 1000;
  if (argc > 1)
  {
    char *endptr = NULL;
    errno = 0;
    long l = strtol(argv[1], &endptr, 0);
    if (*endptr == 0 && errno == 0)
    {
      if (l > 0)
        t = l;
      else if (l == 0)
        t = INFINITE;
    }
  }

  InitializeCriticalSection(&threadarg.mtx);

  HANDLE hThread = (HANDLE)_beginthreadex(NULL, 0, WaitForKeyEvent, (void*)&threadarg, 0, NULL);
  DWORD ret = WaitForSingleObject(hThread, t);
  CloseHandle(hThread);

  EnterCriticalSection(&threadarg.mtx);

  if (threadarg.inRec)
    free(threadarg.inRec);

  if (ret == WAIT_TIMEOUT || threadarg.virtKeyCode == 0)
    threadarg.virtKeyCode = 256;

  if (argc > 2)
  {
    if (_stricmp(argv[2], "x") == 0)
      printf("%x\n", threadarg.virtKeyCode);
    else if (_stricmp(argv[2], "d") == 0)
      printf("%d\n", threadarg.virtKeyCode);
  }

  return threadarg.virtKeyCode;
}


Steffen

neorobin
Posts: 47
Joined: 01 May 2012 12:18

Re: A tool for get Virtual-Key Code of the pressed key

#3 Post by neorobin » 22 Sep 2016 06:26

Thanks for aGerman!

I do not understand those API functions deeply.
I just do some rough code to achieve my purpose.

Post Reply