keytune

Simple virtual like keyboard for Windows
git clone git://git.konyahin.xyz/keytune
Log | Files | Refs | README | LICENSE

keyboard.c (1050B)


      1 //  
      2 // Copyright (c) 2017 Konjahin A.A. <koniahin.ant@yandex.ru>
      3 // Licensed under the MIT License. See LICENSE file in the project root for full license information.  
      4 //
      5 
      6 #include "keyboard.h"
      7 
      8 void setUpInput(INPUT *ip) {
      9   ip->type = INPUT_KEYBOARD;
     10   ip->ki.wScan = 0;
     11   ip->ki.time = 0;
     12   ip->ki.dwExtraInfo = 0;
     13 }
     14 
     15 void hold(INPUT *ip, WORD keyCode) {
     16   setUpInput(ip);
     17   ip->ki.wVk = keyCode;
     18   ip->ki.dwFlags = 0;
     19   SendInput(1, ip, sizeof(INPUT));
     20 }
     21 
     22 void release(INPUT *ip, WORD keyCode) {
     23   setUpInput(ip);
     24   ip->ki.wVk = keyCode;
     25   ip->ki.dwFlags = KEYEVENTF_KEYUP;
     26   SendInput(1, ip, sizeof(INPUT));
     27 }
     28 
     29 void press(INPUT *ip, WORD keyCode) {
     30   hold(ip, keyCode);
     31   release(ip, keyCode);
     32 }
     33 
     34 void getCharForKey(WORD keyCode, wchar_t* buffer, size_t bufferLength) {
     35   HKL keyboardLayout = GetKeyboardLayout(0);
     36   UINT scanCode = MapVirtualKeyEx(keyCode, MAPVK_VK_TO_VSC, keyboardLayout);
     37   
     38   BYTE keyState[256] = {0};
     39   GetKeyboardState(keyState);
     40   
     41   ToUnicodeEx(keyCode, scanCode, keyState, buffer, bufferLength, 0, keyboardLayout);
     42 }