Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef TOINPUT_H_INCLUDED
- #define TOINPUT_H_INCLUDED
- #define WINVER _WIN32_WINNT_WIN7
- #include <exception>
- #include <string>
- #include <windows.h>
- class ToVirtualKey
- {
- private:
- short m_keyData;
- public:
- unsigned char vkey() const
- {return m_keyData & 0xFF;}
- unsigned int scancode() const
- {return MapVirtualKey(vkey(), MAPVK_VK_TO_VSC);}
- bool shift() const
- {return m_keyData & 0x100;}
- bool ctrl() const
- {return m_keyData & 0x200;}
- bool alt() const
- {return m_keyData & 0x400;}
- bool hankaku() const
- {return m_keyData & 0x800;}
- void convert(const char ch)
- {
- m_keyData = VkKeyScanEx(ch, nullptr);
- if (m_keyData == -1)
- throw std::runtime_error("ToVirtualKey() VkKeyScanEx() failed");
- }
- void convert(const unsigned char ch)
- {
- m_keyData = VkKeyScanEx(ch, nullptr);
- if (m_keyData == -1)
- throw std::runtime_error("ToVirtualKey() VkKeyScanEx() failed");
- }
- void convert(const wchar_t ch)
- {
- m_keyData = VkKeyScanExW(ch, nullptr);
- if (m_keyData == -1)
- throw std::runtime_error("ToVirtualKey() VkKeyScanExW() failed");
- }
- ToVirtualKey(const char ch) :
- m_keyData(VkKeyScanEx(ch, nullptr))
- {
- if (m_keyData == -1)
- throw std::runtime_error("ToVirtualKey() VkKeyScanEx() failed");
- }
- ToVirtualKey(const unsigned char ch) :
- m_keyData(VkKeyScanEx(ch, nullptr))
- {
- if (m_keyData == -1)
- throw std::runtime_error("ToVirtualKey() VkKeyScanEx() failed");
- }
- ToVirtualKey(const wchar_t ch) :
- m_keyData(VkKeyScanExW(ch, nullptr))
- {
- if (m_keyData == -1)
- throw std::runtime_error("ToVirtualKey() VkKeyScanExW() failed");
- }
- ToVirtualKey() = default;
- ~ToVirtualKey() = default;
- };
- auto simulateKeyboardOutput(const unsigned char vkey, const bool downPress)
- { // simulate keyboard vkey input
- INPUT in;
- in.type = INPUT_KEYBOARD;
- in.ki.wVk = vkey;
- in.ki.wScan = MapVirtualKey(vkey, MAPVK_VK_TO_VSC);
- in.ki.dwFlags = downPress ? 0 : KEYEVENTF_KEYUP;
- in.ki.dwExtraInfo = 0;
- in.ki.time = 0;
- return SendInput(1, &in, sizeof(in));
- }
- auto simulateKeyboardOutput(const wchar_t unicode, const bool downPress)
- { // simulate keyboard unicode input
- INPUT in;
- in.type = INPUT_KEYBOARD;
- in.ki.wVk = 0;
- in.ki.wScan = unicode;
- in.ki.dwFlags = KEYEVENTF_UNICODE | (downPress ? 0 : KEYEVENTF_KEYUP);
- in.ki.dwExtraInfo = 0;
- in.ki.time = 0;
- return SendInput(1, &in, sizeof(in));
- }
- void keyPress(const unsigned char vkey)
- {
- simulateKeyboardOutput(vkey, true);
- simulateKeyboardOutput(vkey, false);
- }
- void keyPress(const wchar_t unicode)
- {
- simulateKeyboardOutput(unicode, true);
- simulateKeyboardOutput(unicode, false);
- }
- void stringToInput(const std::string& ansi)
- {
- for(const unsigned char& c : ansi)
- {
- ToVirtualKey virtkey;
- virtkey.convert(c);
- auto modifiers = [&virtkey](bool downpress)
- {
- constexpr const unsigned char shift = VK_SHIFT;
- if (virtkey.shift()) simulateKeyboardOutput(shift, downpress);
- constexpr const unsigned char ctrl = VK_CONTROL;
- if (virtkey.ctrl()) simulateKeyboardOutput(ctrl, downpress);
- constexpr const unsigned char alt = VK_MENU;
- if (virtkey.alt()) simulateKeyboardOutput(alt, downpress);
- };
- modifiers(true);
- keyPress(virtkey.vkey());
- modifiers(false);
- }
- }
- void stringToInput(const std::wstring& unicode)
- {for(const wchar_t& c : unicode) keyPress(c);}
- #endif // TOINPUT_H_INCLUDED
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement