Skip to content

Commit

Permalink
Add jngl::KeyboardShortcut
Browse files Browse the repository at this point in the history
  • Loading branch information
jhasse committed Feb 19, 2025
1 parent 450dbef commit b355c56
Show file tree
Hide file tree
Showing 5 changed files with 185 additions and 41 deletions.
81 changes: 41 additions & 40 deletions src/jngl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,43 +7,44 @@
#include "jngl/Achievement.hpp" // IWYU pragma: keep
#include "jngl/Alpha.hpp" // IWYU pragma: keep
#include "jngl/Channel.hpp"
#include "jngl/Color.hpp" // IWYU pragma: keep
#include "jngl/Container.hpp" // IWYU pragma: keep
#include "jngl/Controller.hpp" // IWYU pragma: keep
#include "jngl/Drawable.hpp" // IWYU pragma: keep
#include "jngl/FrameBuffer.hpp" // IWYU pragma: keep
#include "jngl/ImageData.hpp" // IWYU pragma: keep
#include "jngl/Label.hpp" // IWYU pragma: keep
#include "jngl/OutlinedFont.hpp" // IWYU pragma: keep
#include "jngl/Pixels.hpp" // IWYU pragma: keep
#include "jngl/Rect.hpp" // IWYU pragma: keep
#include "jngl/Rgb.hpp" // IWYU pragma: keep
#include "jngl/Rgba.hpp" // IWYU pragma: keep
#include "jngl/ScaleablePixels.hpp" // IWYU pragma: keep
#include "jngl/Shader.hpp" // IWYU pragma: keep
#include "jngl/ShaderProgram.hpp" // IWYU pragma: keep
#include "jngl/Singleton.hpp" // IWYU pragma: keep
#include "jngl/SoundFile.hpp" // IWYU pragma: keep
#include "jngl/Sprite.hpp" // IWYU pragma: keep
#include "jngl/TextLine.hpp" // IWYU pragma: keep
#include "jngl/Vertex.hpp" // IWYU pragma: keep
#include "jngl/Video.hpp" // IWYU pragma: keep
#include "jngl/Widget.hpp" // IWYU pragma: keep
#include "jngl/WorkFactory.hpp" // IWYU pragma: keep
#include "jngl/debug.hpp" // IWYU pragma: keep
#include "jngl/effects.hpp" // IWYU pragma: keep
#include "jngl/font.hpp" // IWYU pragma: keep
#include "jngl/input.hpp" // IWYU pragma: keep
#include "jngl/job.hpp" // IWYU pragma: keep
#include "jngl/log.hpp" // IWYU pragma: keep
#include "jngl/main.hpp" // IWYU pragma: keep
#include "jngl/matrix.hpp" // IWYU pragma: keep
#include "jngl/message.hpp" // IWYU pragma: keep
#include "jngl/other.hpp" // IWYU pragma: keep
#include "jngl/screen.hpp" // IWYU pragma: keep
#include "jngl/shapes.hpp" // IWYU pragma: keep
#include "jngl/sound.hpp" // IWYU pragma: keep
#include "jngl/text.hpp" // IWYU pragma: keep
#include "jngl/time.hpp" // IWYU pragma: keep
#include "jngl/window.hpp" // IWYU pragma: keep
#include "jngl/work.hpp" // IWYU pragma: keep
#include "jngl/Color.hpp" // IWYU pragma: keep
#include "jngl/Container.hpp" // IWYU pragma: keep
#include "jngl/Controller.hpp" // IWYU pragma: keep
#include "jngl/Drawable.hpp" // IWYU pragma: keep
#include "jngl/FrameBuffer.hpp" // IWYU pragma: keep
#include "jngl/ImageData.hpp" // IWYU pragma: keep
#include "jngl/KeyboardShortcut.hpp" // IWYU pragma: keep
#include "jngl/Label.hpp" // IWYU pragma: keep
#include "jngl/OutlinedFont.hpp" // IWYU pragma: keep
#include "jngl/Pixels.hpp" // IWYU pragma: keep
#include "jngl/Rect.hpp" // IWYU pragma: keep
#include "jngl/Rgb.hpp" // IWYU pragma: keep
#include "jngl/Rgba.hpp" // IWYU pragma: keep
#include "jngl/ScaleablePixels.hpp" // IWYU pragma: keep
#include "jngl/Shader.hpp" // IWYU pragma: keep
#include "jngl/ShaderProgram.hpp" // IWYU pragma: keep
#include "jngl/Singleton.hpp" // IWYU pragma: keep
#include "jngl/SoundFile.hpp" // IWYU pragma: keep
#include "jngl/Sprite.hpp" // IWYU pragma: keep
#include "jngl/TextLine.hpp" // IWYU pragma: keep
#include "jngl/Vertex.hpp" // IWYU pragma: keep
#include "jngl/Video.hpp" // IWYU pragma: keep
#include "jngl/Widget.hpp" // IWYU pragma: keep
#include "jngl/WorkFactory.hpp" // IWYU pragma: keep
#include "jngl/debug.hpp" // IWYU pragma: keep
#include "jngl/effects.hpp" // IWYU pragma: keep
#include "jngl/font.hpp" // IWYU pragma: keep
#include "jngl/input.hpp" // IWYU pragma: keep
#include "jngl/job.hpp" // IWYU pragma: keep
#include "jngl/log.hpp" // IWYU pragma: keep
#include "jngl/main.hpp" // IWYU pragma: keep
#include "jngl/matrix.hpp" // IWYU pragma: keep
#include "jngl/message.hpp" // IWYU pragma: keep
#include "jngl/other.hpp" // IWYU pragma: keep
#include "jngl/screen.hpp" // IWYU pragma: keep
#include "jngl/shapes.hpp" // IWYU pragma: keep
#include "jngl/sound.hpp" // IWYU pragma: keep
#include "jngl/text.hpp" // IWYU pragma: keep
#include "jngl/time.hpp" // IWYU pragma: keep
#include "jngl/window.hpp" // IWYU pragma: keep
#include "jngl/work.hpp" // IWYU pragma: keep
44 changes: 44 additions & 0 deletions src/jngl/KeyboardShortcut.cpp
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
32 changes: 32 additions & 0 deletions src/jngl/KeyboardShortcut.hpp
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
63 changes: 62 additions & 1 deletion src/jngl/input.cpp
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);
}
Expand Down
6 changes: 6 additions & 0 deletions src/jngl/input.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ enum KeyType : uint8_t {
Space,
ShiftL,
ShiftR,

/// Shortcut for either ShiftL or ShiftR
Shift,

F1,
F2,
F3,
Expand All @@ -73,6 +77,8 @@ enum KeyType : uint8_t {
};
} // namespace key

std::string keyToString(key::KeyType);

/// Display onscreen keyboard for touch devices
void setKeyboardVisible(bool);

Expand Down

0 comments on commit b355c56

Please sign in to comment.