Skip to content

Commit

Permalink
[os.input] add tz::os::is_key_pressed to query if a keyboard key is c…
Browse files Browse the repository at this point in the history
…urrently pressed
  • Loading branch information
harrand committed Oct 20, 2024
1 parent 792e4a1 commit 06c9818
Show file tree
Hide file tree
Showing 2 changed files with 200 additions and 10 deletions.
75 changes: 65 additions & 10 deletions include/tz/os/input.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,64 @@ namespace tz::os
**/
enum class key
{
// Letters
a, b, c, d, e, f, g,
h, i, j, k, l, m, n,
o, p, q, r, s, t, u,
v, w, x, y, z,

// Numbers
num_0, num_1, num_2, num_3, num_4,
num_5, num_6, num_7, num_8, num_9,
undefined
backspace,
tab,
enter,

left_shift,
left_control,
left_alt,

pause,
caps_lock,
escape,

spacebar,
page_down,
page_up,

end,
home,

left_arrow,
up_arrow,
right_arrow,
down_arrow,

select,
print,
printsc,
ins,
del,
n0,n1,n2,n3,n4,n5,n6,n7,n8,n9,
a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,

left_winkey,
right_winkey,
apps,
sleep,

np0,np1,np2,np3,np4,np5,np6,np7,np8,np9,
npmul,npadd,npsep,npsub,npdec,npdiv,
f1,f2,f3,f4,f5,f6,f7,f8,f9,f10,f11,f12,f13,f14,f15,f16,f17,f18,f19,f20,f21,f22,f23,f24,
num_lock,
scroll_lock,

right_shift,
right_control,
right_alt,
semicol,
period,
plus,
minus,

obrack,
cbrack,
forward_slash,
back_slash,
apostrophe,
grave,
_count
};

using char_type_callback = void(*)(char);
Expand All @@ -36,6 +84,13 @@ namespace tz::os
* @return - @ref error_code::precondition_failure if a window has not yet been opened.
**/
tz::error_code install_char_typed_callback(char_type_callback callback);
/**
* @ingroup tz_os_input
* @brief Query as to whether a specific key is pressed right now.
* @param k Keyboard key to query
* @return True if the given key is currently pressed, otherwise false.
*/
bool is_key_pressed(key k);
}

#endif // TOPAZ_OS_INPUT_HPP
135 changes: 135 additions & 0 deletions src/tz/os/impl_win32.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,141 @@ namespace tz::os
return tz::error_code::success;
}

bool key_pressed(key k)
{
std::array<int, static_cast<int>(key::_count)> key_mappings
{
VK_BACK,
VK_TAB,
VK_RETURN,

VK_LSHIFT,
VK_LCONTROL,
VK_LMENU,

VK_PAUSE,
VK_CAPITAL,
VK_ESCAPE,

VK_SPACE,
VK_NEXT,
VK_PRIOR,

VK_END,
VK_HOME,

VK_LEFT,
VK_UP,
VK_RIGHT,
VK_DOWN,

VK_SELECT,
VK_PRINT,
VK_SNAPSHOT,
VK_INSERT,
VK_DELETE,
'0',
'1',
'2',
'3',
'4',
'5',
'6',
'7',
'8',
'9',
'A',
'B',
'C',
'D',
'E',
'F',
'G',
'H',
'I',
'J',
'K',
'L',
'M',
'N',
'O',
'P',
'Q',
'R',
'S',
'T',
'U',
'V',
'W',
'X',
'Y',
'Z',
VK_LWIN,
VK_RWIN,
VK_APPS,
VK_SLEEP,

VK_NUMPAD0,
VK_NUMPAD1,
VK_NUMPAD2,
VK_NUMPAD3,
VK_NUMPAD4,
VK_NUMPAD5,
VK_NUMPAD6,
VK_NUMPAD7,
VK_NUMPAD8,
VK_NUMPAD9,
VK_MULTIPLY,
VK_ADD,
VK_SEPARATOR,
VK_SUBTRACT,
VK_DECIMAL,
VK_DIVIDE,
VK_F1,
VK_F2,
VK_F3,
VK_F4,
VK_F5,
VK_F6,
VK_F7,
VK_F8,
VK_F9,
VK_F10,
VK_F11,
VK_F12,
VK_F13,
VK_F14,
VK_F15,
VK_F16,
VK_F17,
VK_F18,
VK_F19,
VK_F20,
VK_F21,
VK_F22,
VK_F23,
VK_F24,
VK_NUMLOCK,
VK_SCROLL,

VK_RSHIFT,
VK_RCONTROL,
VK_RMENU,
VK_OEM_1,
VK_OEM_PERIOD,
VK_OEM_PLUS,
VK_OEM_MINUS,

VK_OEM_4,
VK_OEM_6,
VK_OEM_2,
VK_OEM_5,
VK_OEM_7,
VK_OEM_3
};
return GetAsyncKeyState(key_mappings[static_cast<int>(k)]) & 0x8000;
}

std::expected<std::string, tz::error_code> read_file(std::filesystem::path path)
{
HANDLE file = CreateFileA(path.string().c_str(), GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr);
Expand Down

0 comments on commit 06c9818

Please sign in to comment.