Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma once
- #include "ulib_shared.h"
- namespace ulib
- {
- namespace io
- {
- enum key_flag
- {
- FLAG_KEY = 0,
- FLAG_MOUSE = 1,
- };
- // Print - string
- template <typename T>
- void Print(const T& data)
- {
- if constexpr (std::is_convertible_v<T, std::string>)
- std::cout << data;
- else if constexpr (std::is_convertible_v<T, std::wstring>)
- std::wcout << data;
- }
- // Print - vector
- template <typename T>
- void Print(const std::vector<T>& data)
- {
- if constexpr (std::is_convertible_v<T, std::string>)
- for(auto& string : data)
- std::cout << string << "\n";
- else if constexpr (std::is_convertible_v<T, std::wstring>)
- for(auto& string : data)
- std::wcout << string << "\n";
- }
- // PlaySound
- template <typename T>
- const bool PlaySound(const T& file_path)
- {
- if constexpr (std::is_convertible_v<T, std::string>)
- {
- std::ifstream file_stream(file_path);
- if (!file_stream.is_open())
- return false;
- }
- else if constexpr (std::is_convertible_v<T, std::wstring>)
- {
- std::wifstream file_stream(file_path);
- if (!file_stream.is_open())
- return false;
- }
- const T command = "play " + file_path;
- mciSendString(command.c_str(), NULL, 0, NULL);
- return true;
- }
- // Check
- const bool Check(const key_code& key, const key_flag& flag = FLAG_KEY, const bool& async = false)
- {
- if (flag == FLAG_KEY)
- {
- if (!async)
- if (GetKeyState(key) & 0x8000)
- {
- FlushConsoleInputBuffer(GetStdHandle(STD_INPUT_HANDLE));
- return true;
- }
- if (GetAsyncKeyState(key) & 0x8000)
- {
- FlushConsoleInputBuffer(GetStdHandle(STD_INPUT_HANDLE));
- return true;
- }
- }
- else if (flag == FLAG_MOUSE)
- {
- if (!async)
- if (GetKeyState(key) < 0)
- return true;
- if (GetAsyncKeyState(key) < 0)
- return true;
- }
- return false;
- }
- // Press
- void Press(const key_code& key, const key_flag& flag = FLAG_KEY, const time_ms& wait = 10, const HWND& window = NULL)
- {
- if (flag == FLAG_KEY)
- {
- if (window != NULL)
- {
- SendMessage(window, WM_KEYDOWN, key, 0);
- Sleep(wait);
- SendMessage(window, WM_KEYUP, key, 0);
- }
- else
- {
- INPUT ip = { 0 };
- ip.type = INPUT_KEYBOARD;
- ip.ki.wVk = key;
- SendInput(1, &ip, sizeof(INPUT));
- Sleep(wait);
- ip.ki.dwFlags = KEYEVENTF_KEYUP;
- SendInput(1, &ip, sizeof(INPUT));
- }
- }
- else if (flag == FLAG_MOUSE)
- if (window != NULL)
- {
- SendMessage(window, WM_KEYDOWN, key, 0);
- Sleep(wait);
- SendMessage(window, WM_KEYUP, key, 0);
- }
- }
- // Hold
- void Hold(const key_code& key, const key_flag& flag = FLAG_KEY, const HWND& window = NULL)
- {
- if (flag == FLAG_KEY)
- {
- if (window != NULL)
- SendMessage(window, WM_KEYDOWN, key, 0);
- else
- {
- INPUT ip = { 0 };
- ip.type = INPUT_KEYBOARD;
- ip.ki.wVk = key;
- SendInput(1, &ip, sizeof(INPUT));
- }
- }
- }
- // Release
- void Release(const key_code& key, const key_flag& flag = FLAG_KEY, const HWND& window = NULL)
- {
- if (flag == FLAG_KEY)
- {
- if (window != NULL)
- SendMessage(window, WM_KEYUP, key, 0);
- else
- {
- INPUT ip = { 0 };
- ip.type = INPUT_KEYBOARD;
- ip.ki.wVk = key;
- ip.ki.dwFlags = KEYEVENTF_KEYUP;
- SendInput(1, &ip, sizeof(INPUT));
- }
- }
- }
- // SetCursorPosition
- void SetCursorPosition(const std::pair<int, int>& position)
- {
- SetCursorPos(position.first, position.second);
- }
- // GetCursorPosition
- const integral_position GetCursorPosition()
- {
- POINT cursor_pos;
- GetCursorPos(&cursor_pos);
- return { cursor_pos.x, cursor_pos.y };
- }
- // ShowConsoleCursor
- void DisplayConsoleCursor(bool to_display)
- {
- HANDLE output_handle = GetStdHandle(STD_OUTPUT_HANDLE);
- CONSOLE_CURSOR_INFO cursor_info;
- GetConsoleCursorInfo(output_handle, &cursor_info);
- if (to_display == true)
- cursor_info.bVisible = true;
- else
- cursor_info.bVisible = false;
- SetConsoleCursorInfo(output_handle, &cursor_info);
- }
- }
- using namespace io;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement