Advertisement
alaestor

[FGL Utility] ToInput.h

Oct 1st, 2017
262
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.37 KB | None | 0 0
  1. #ifndef TOINPUT_H_INCLUDED
  2. #define TOINPUT_H_INCLUDED
  3. #define WINVER _WIN32_WINNT_WIN7
  4. #include <exception>
  5. #include <string>
  6. #include <windows.h>
  7.  
  8. class ToVirtualKey
  9. {
  10.     private:
  11.  
  12.     short m_keyData;
  13.  
  14.     public:
  15.  
  16.     unsigned char vkey() const
  17.     {return m_keyData & 0xFF;}
  18.  
  19.     unsigned int scancode() const
  20.     {return MapVirtualKey(vkey(), MAPVK_VK_TO_VSC);}
  21.  
  22.     bool shift() const
  23.     {return m_keyData & 0x100;}
  24.  
  25.     bool ctrl() const
  26.     {return m_keyData & 0x200;}
  27.  
  28.     bool alt() const
  29.     {return m_keyData & 0x400;}
  30.  
  31.     bool hankaku() const
  32.     {return m_keyData & 0x800;}
  33.  
  34.     void convert(const char ch)
  35.     {
  36.         m_keyData = VkKeyScanEx(ch, nullptr);
  37.         if (m_keyData == -1)
  38.             throw std::runtime_error("ToVirtualKey() VkKeyScanEx() failed");
  39.     }
  40.  
  41.     void convert(const unsigned char ch)
  42.     {
  43.         m_keyData = VkKeyScanEx(ch, nullptr);
  44.         if (m_keyData == -1)
  45.             throw std::runtime_error("ToVirtualKey() VkKeyScanEx() failed");
  46.     }
  47.  
  48.     void convert(const wchar_t ch)
  49.     {
  50.         m_keyData = VkKeyScanExW(ch, nullptr);
  51.         if (m_keyData == -1)
  52.             throw std::runtime_error("ToVirtualKey() VkKeyScanExW() failed");
  53.     }
  54.  
  55.     ToVirtualKey(const char ch) :
  56.         m_keyData(VkKeyScanEx(ch, nullptr))
  57.     {
  58.         if (m_keyData == -1)
  59.             throw std::runtime_error("ToVirtualKey() VkKeyScanEx() failed");
  60.     }
  61.  
  62.     ToVirtualKey(const unsigned char ch) :
  63.         m_keyData(VkKeyScanEx(ch, nullptr))
  64.     {
  65.         if (m_keyData == -1)
  66.             throw std::runtime_error("ToVirtualKey() VkKeyScanEx() failed");
  67.     }
  68.  
  69.     ToVirtualKey(const wchar_t ch) :
  70.         m_keyData(VkKeyScanExW(ch, nullptr))
  71.     {
  72.         if (m_keyData == -1)
  73.             throw std::runtime_error("ToVirtualKey() VkKeyScanExW() failed");
  74.     }
  75.  
  76.     ToVirtualKey() = default;
  77.     ~ToVirtualKey() = default;
  78. };
  79.  
  80. auto simulateKeyboardOutput(const unsigned char vkey, const bool downPress)
  81. { // simulate keyboard vkey input
  82.     INPUT in;
  83.     in.type = INPUT_KEYBOARD;
  84.     in.ki.wVk = vkey;
  85.     in.ki.wScan = MapVirtualKey(vkey, MAPVK_VK_TO_VSC);
  86.     in.ki.dwFlags = downPress ? 0 : KEYEVENTF_KEYUP;
  87.     in.ki.dwExtraInfo = 0;
  88.     in.ki.time = 0;
  89.  
  90.     return SendInput(1, &in, sizeof(in));
  91. }
  92.  
  93. auto simulateKeyboardOutput(const wchar_t unicode, const bool downPress)
  94. { // simulate keyboard unicode input
  95.     INPUT in;
  96.     in.type = INPUT_KEYBOARD;
  97.     in.ki.wVk = 0;
  98.     in.ki.wScan = unicode;
  99.     in.ki.dwFlags = KEYEVENTF_UNICODE | (downPress ? 0 : KEYEVENTF_KEYUP);
  100.     in.ki.dwExtraInfo = 0;
  101.     in.ki.time = 0;
  102.  
  103.     return SendInput(1, &in, sizeof(in));
  104. }
  105.  
  106. void keyPress(const unsigned char vkey)
  107. {
  108.     simulateKeyboardOutput(vkey, true);
  109.     simulateKeyboardOutput(vkey, false);
  110. }
  111.  
  112. void keyPress(const wchar_t unicode)
  113. {
  114.     simulateKeyboardOutput(unicode, true);
  115.     simulateKeyboardOutput(unicode, false);
  116. }
  117.  
  118. void stringToInput(const std::string& ansi)
  119. {
  120.     for(const unsigned char& c : ansi)
  121.     {
  122.         ToVirtualKey virtkey;
  123.         virtkey.convert(c);
  124.  
  125.         auto modifiers = [&virtkey](bool downpress)
  126.         {
  127.             constexpr const unsigned char shift = VK_SHIFT;
  128.             if (virtkey.shift()) simulateKeyboardOutput(shift, downpress);
  129.             constexpr const unsigned char ctrl = VK_CONTROL;
  130.             if (virtkey.ctrl()) simulateKeyboardOutput(ctrl, downpress);
  131.             constexpr const unsigned char alt = VK_MENU;
  132.             if (virtkey.alt()) simulateKeyboardOutput(alt, downpress);
  133.         };
  134.  
  135.         modifiers(true);
  136.         keyPress(virtkey.vkey());
  137.         modifiers(false);
  138.     }
  139. }
  140.  
  141. void stringToInput(const std::wstring& unicode)
  142. {for(const wchar_t& c : unicode) keyPress(c);}
  143.  
  144. #endif // TOINPUT_H_INCLUDED
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement