Advertisement
Guest User

Untitled

a guest
Jun 15th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.77 KB | None | 0 0
  1. #pragma once
  2. #include "ulib_shared.h"
  3.  
  4.  
  5. namespace ulib
  6. {
  7.  
  8.     namespace io
  9.     {
  10.         enum key_flag
  11.         {
  12.             FLAG_KEY   = 0,
  13.             FLAG_MOUSE = 1,
  14.         };
  15.  
  16.  
  17.         // Print - string
  18.         template <typename T>
  19.         void Print(const T& data)
  20.         {
  21.             if constexpr (std::is_convertible_v<T, std::string>)
  22.                 std::cout << data;
  23.  
  24.             else if constexpr (std::is_convertible_v<T, std::wstring>)
  25.                 std::wcout << data;
  26.         }
  27.  
  28.         // Print - vector
  29.         template <typename T>
  30.         void Print(const std::vector<T>& data)
  31.         {
  32.             if constexpr (std::is_convertible_v<T, std::string>)
  33.                 for(auto& string : data)
  34.                     std::cout << string << "\n";
  35.  
  36.             else if constexpr (std::is_convertible_v<T, std::wstring>)
  37.                 for(auto& string : data)
  38.                     std::wcout << string << "\n";
  39.         }
  40.  
  41.  
  42.         // PlaySound
  43.         template <typename T>
  44.         const bool PlaySound(const T& file_path)
  45.         {
  46.             if constexpr (std::is_convertible_v<T, std::string>)
  47.             {
  48.                 std::ifstream file_stream(file_path);
  49.                 if (!file_stream.is_open())
  50.                     return false;
  51.             }
  52.             else if constexpr (std::is_convertible_v<T, std::wstring>)
  53.             {
  54.                 std::wifstream file_stream(file_path);
  55.                 if (!file_stream.is_open())
  56.                     return false;
  57.             }
  58.  
  59.             const T command = "play " + file_path;
  60.             mciSendString(command.c_str(), NULL, 0, NULL);
  61.  
  62.             return true;
  63.         }
  64.  
  65.  
  66.         // Check
  67.         const bool Check(const key_code& key, const key_flag& flag = FLAG_KEY, const bool& async = false)
  68.         {
  69.             if (flag == FLAG_KEY)
  70.             {
  71.                 if (!async)
  72.                     if (GetKeyState(key) & 0x8000)
  73.                     {
  74.                         FlushConsoleInputBuffer(GetStdHandle(STD_INPUT_HANDLE));
  75.                         return true;
  76.                     }
  77.  
  78.                 if (GetAsyncKeyState(key) & 0x8000)
  79.                 {
  80.                     FlushConsoleInputBuffer(GetStdHandle(STD_INPUT_HANDLE));
  81.                     return true;
  82.                 }
  83.             }
  84.  
  85.             else if (flag == FLAG_MOUSE)
  86.             {
  87.                 if (!async)
  88.                     if (GetKeyState(key) < 0)
  89.                         return true;
  90.  
  91.             if (GetAsyncKeyState(key) < 0)
  92.                 return true;
  93.             }
  94.  
  95.             return false;
  96.         }
  97.  
  98.  
  99.         // Press
  100.         void Press(const key_code& key, const key_flag& flag = FLAG_KEY, const time_ms& wait = 10, const HWND& window = NULL)
  101.         {
  102.             if (flag == FLAG_KEY)
  103.             {
  104.                 if (window != NULL)
  105.                 {
  106.                     SendMessage(window, WM_KEYDOWN, key, 0);
  107.                     Sleep(wait);
  108.                     SendMessage(window, WM_KEYUP, key, 0);
  109.                 }
  110.                 else
  111.                 {
  112.                     INPUT ip = { 0 };
  113.                     ip.type = INPUT_KEYBOARD;
  114.                     ip.ki.wVk = key;
  115.  
  116.                     SendInput(1, &ip, sizeof(INPUT));
  117.                     Sleep(wait);
  118.  
  119.                     ip.ki.dwFlags = KEYEVENTF_KEYUP;
  120.                     SendInput(1, &ip, sizeof(INPUT));
  121.                 }
  122.             }
  123.             else if (flag == FLAG_MOUSE)
  124.                 if (window != NULL)
  125.                 {
  126.                     SendMessage(window, WM_KEYDOWN, key, 0);
  127.                     Sleep(wait);
  128.                     SendMessage(window, WM_KEYUP, key, 0);
  129.                 }
  130.         }
  131.  
  132.  
  133.         // Hold
  134.         void Hold(const key_code& key, const key_flag& flag = FLAG_KEY, const HWND& window = NULL)
  135.         {
  136.             if (flag == FLAG_KEY)
  137.             {
  138.                 if (window != NULL)
  139.                     SendMessage(window, WM_KEYDOWN, key, 0);
  140.                 else
  141.             {
  142.                     INPUT ip = { 0 };
  143.                     ip.type = INPUT_KEYBOARD;
  144.                     ip.ki.wVk = key;
  145.                     SendInput(1, &ip, sizeof(INPUT));
  146.                 }
  147.             }
  148.         }
  149.  
  150.  
  151.         // Release
  152.         void Release(const key_code& key, const key_flag& flag = FLAG_KEY, const HWND& window = NULL)
  153.         {
  154.             if (flag == FLAG_KEY)
  155.             {
  156.                 if (window != NULL)
  157.                     SendMessage(window, WM_KEYUP, key, 0);
  158.                 else
  159.                 {
  160.                     INPUT ip = { 0 };
  161.                     ip.type = INPUT_KEYBOARD;
  162.                     ip.ki.wVk = key;
  163.                     ip.ki.dwFlags = KEYEVENTF_KEYUP;
  164.                     SendInput(1, &ip, sizeof(INPUT));
  165.                 }
  166.             }
  167.         }
  168.  
  169.  
  170.         // SetCursorPosition
  171.         void SetCursorPosition(const std::pair<int, int>& position)
  172.         {
  173.             SetCursorPos(position.first, position.second);
  174.         }
  175.  
  176.         // GetCursorPosition
  177.         const integral_position GetCursorPosition()
  178.         {
  179.             POINT cursor_pos;
  180.             GetCursorPos(&cursor_pos);
  181.  
  182.             return { cursor_pos.x, cursor_pos.y };
  183.         }
  184.  
  185.  
  186.         // ShowConsoleCursor
  187.         void DisplayConsoleCursor(bool to_display)
  188.         {
  189.             HANDLE output_handle = GetStdHandle(STD_OUTPUT_HANDLE);
  190.             CONSOLE_CURSOR_INFO cursor_info;
  191.  
  192.             GetConsoleCursorInfo(output_handle, &cursor_info);
  193.  
  194.             if (to_display == true)
  195.                 cursor_info.bVisible = true;
  196.             else
  197.                 cursor_info.bVisible = false;
  198.  
  199.             SetConsoleCursorInfo(output_handle, &cursor_info);
  200.         }
  201.  
  202.     }
  203.     using namespace io;
  204.  
  205. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement