-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
185 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// Copyright 2025 Jan Niklas Hasse <[email protected]> | ||
// For conditions of distribution and use, see copyright notice in LICENSE.txt | ||
#include "KeyboardShortcut.hpp" | ||
|
||
namespace jngl { | ||
|
||
KeyboardShortcut::KeyboardShortcut() : key('\0') { | ||
} | ||
|
||
KeyboardShortcut::KeyboardShortcut(key::KeyType modifier, char key) | ||
: modifiers({ | ||
modifier, | ||
}), | ||
key(key) { | ||
} | ||
KeyboardShortcut::KeyboardShortcut(key::KeyType modifier1, key::KeyType modifier2, char key) | ||
: modifiers({ | ||
modifier1, | ||
modifier2, | ||
}), | ||
key(key) { | ||
} | ||
|
||
KeyboardShortcut::KeyboardShortcut(key::KeyType key) : key(key) { | ||
} | ||
|
||
std::string KeyboardShortcut::toString() const { | ||
std::string result; | ||
for (const auto modifier : modifiers) { | ||
result += keyToString(modifier) + "+"; | ||
} | ||
if (std::holds_alternative<key::KeyType>(key)) { | ||
result += keyToString(std::get<key::KeyType>(key)); | ||
} else { | ||
char k = std::get<char>(key); | ||
if (k == '\0') { | ||
return result; | ||
} | ||
result += static_cast<char>(std::toupper(k)); | ||
} | ||
return result; | ||
} | ||
|
||
} // namespace jngl |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// Copyright 2025 Jan Niklas Hasse <[email protected]> | ||
// For conditions of distribution and use, see copyright notice in LICENSE.txt | ||
/// Contains jngl::KeyboardShortcut class | ||
/// @file | ||
#pragma once | ||
|
||
#include "input.hpp" | ||
|
||
namespace jngl { | ||
|
||
class KeyboardShortcut { | ||
public: | ||
/// no shortcut, will never trigger | ||
KeyboardShortcut(); | ||
|
||
/// modifier + key, e.g. { jngl::key::Control, 's' } for Ctrl+S | ||
KeyboardShortcut(key::KeyType modifier, char key); | ||
|
||
/// two modifiers + key, e.g. { jngl::key::Control, jngl::key::Shift, 's' } for Ctrl+Shift+S | ||
KeyboardShortcut(key::KeyType modifier1, key::KeyType modifier2, char key); | ||
|
||
// single key, e.g. jngl::key::F1 | ||
KeyboardShortcut(key::KeyType key); // NOLINT | ||
|
||
std::string toString() const; | ||
|
||
private: | ||
std::vector<key::KeyType> modifiers; | ||
std::variant<key::KeyType, char> key; | ||
}; | ||
|
||
} // namespace jngl |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,71 @@ | ||
// Copyright 2012-2024 Jan Niklas Hasse <[email protected]> | ||
// Copyright 2012-2025 Jan Niklas Hasse <[email protected]> | ||
// For conditions of distribution and use, see copyright notice in LICENSE.txt | ||
|
||
#include "../windowptr.hpp" | ||
|
||
namespace jngl { | ||
|
||
std::string keyToString(key::KeyType key) { | ||
switch (key) { | ||
case key::Ctrl: | ||
return "Ctrl"; | ||
case key::Shift: | ||
return "Shift"; | ||
case key::F1: | ||
return "F1"; | ||
case key::F2: | ||
return "F2"; | ||
case key::F3: | ||
return "F3"; | ||
case key::F4: | ||
return "F4"; | ||
case key::F5: | ||
return "F5"; | ||
case key::F6: | ||
return "F6"; | ||
case key::F7: | ||
return "F7"; | ||
case key::F8: | ||
return "F8"; | ||
case key::F9: | ||
return "F9"; | ||
case key::F10: | ||
return "F10"; | ||
case key::F11: | ||
return "F11"; | ||
case key::F12: | ||
return "F12"; | ||
case key::Left: | ||
case key::Up: | ||
case key::Right: | ||
case key::Down: | ||
case key::PageUp: | ||
case key::PageDown: | ||
case key::Home: | ||
case key::End: | ||
case key::BackSpace: | ||
case key::Tab: | ||
case key::Clear: | ||
case key::Return: | ||
case key::Pause: | ||
case key::Escape: | ||
case key::Delete: | ||
case key::ControlL: | ||
case key::ControlR: | ||
case key::CapsLock: | ||
case key::AltL: | ||
case key::AltR: | ||
case key::SuperL: | ||
case key::SuperR: | ||
case key::Space: | ||
case key::ShiftL: | ||
case key::ShiftR: | ||
case key::Any: | ||
break; | ||
} | ||
throw std::runtime_error("Unknown key"); | ||
} | ||
|
||
void setKeyPressed(key::KeyType key, bool p) { | ||
pWindow->setKeyPressed(key, p); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters