Skip to content

Commit

Permalink
Merge pull request #316 from GLVis/najlkin/fix-alt-key
Browse files Browse the repository at this point in the history
Fixed Alt handling to allow layout translation and documenting Ctrl+A
  • Loading branch information
najlkin authored Sep 20, 2024
2 parents 7053ae2 + e6fc8b2 commit d27f2ed
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 11 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,11 @@ Key commands
- <kbd>F6</kbd> – Palette menu (negative repeat number flips the palette)
- <kbd>F7</kbd> – Change the minimum and maximum values
- <kbd>Shift</kbd> + <kbd>F7</kbd> – Set the bounding box from the terminal
- <kbd>Ctrl</kbd> + <kbd>a</kbd> – Cycle through the auto-scaling options:
- `off`: do not change the bounding box and the value range
- `on` (default): recompute both the bounding box and the value range
- `value`: recompute only the value range
- `mesh`: recompute only the bounding box

## 2D scalar data

Expand Down
40 changes: 29 additions & 11 deletions lib/sdl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -270,18 +270,21 @@ void SdlWindow::mouseEventUp(SDL_MouseButtonEvent& eb)
void SdlWindow::keyDownEvent(SDL_Keysym& ks)
{
// Some keyDown events will be followed by a textInput event which will
// handle key translation due to Shift or CapsLock, so we leave such events
// to be processed there.
// handle key translation due to Shift, CapsLock or AltGr, so we leave such
// events to be processed there.
// Note: the same condition has to be used in signalKeyDown().
const char *scan_name = SDL_GetScancodeName(ks.scancode);
if ((scan_name[0] >= 32 && scan_name[0] < 127) && scan_name[1] == '\0'
&& (ks.mod & (KMOD_CTRL | KMOD_ALT | KMOD_GUI)) == 0)
&& (ks.mod & (KMOD_CTRL | KMOD_LALT | KMOD_GUI)) == 0)
{
lastKeyDownProcessed = false;
lastKeyDownMods = ks.mod;
lastKeyDownChar = ks.sym;
return;
}
// If any 'mod' key other than KMOD_SHIFT or KMOD_CAPS is pressed, or the key
// is not in the range [32,127) then we processed the event here.
// If any 'mod' key other than KMOD_SHIFT, KMOD_CAPS or KMOD_RALT is
// pressed, or the key is not in the range [32,127) then we processed the
// event here.
lastKeyDownProcessed = true;
if (onKeyDown[ks.sym])
{
Expand Down Expand Up @@ -310,16 +313,31 @@ void SdlWindow::textInputEvent(const SDL_TextInputEvent &tie)
// This event follows a keyDown event where we've recorded if the event was
// processed in keyDownEvent(). If it was not processed, we do it here.
if (lastKeyDownProcessed) { return; }
const char c = tie.text[0];
char c = tie.text[0];
if (!onKeyDown[c])
{
// If the key was translated to something that is not handled, return to
// the physical key passed in the keyDown event.
c = lastKeyDownChar;
}
if (onKeyDown[c])
{
// Keys with 'mods' (other than Shift and CapsLock) are processed in
// keyDownEvent().
const int mods = 0;
onKeyDown[c](mods);
onKeyDown[c](lastKeyDownMods & ~(KMOD_CAPS | KMOD_LSHIFT | KMOD_RSHIFT));

// Record the key in 'saved_keys':
bool isAlt = lastKeyDownMods & (KMOD_ALT);
bool isCtrl = lastKeyDownMods & (KMOD_CTRL);
if (isAlt || isCtrl)
{
saved_keys += "[";
}
if (isCtrl) { saved_keys += "C-"; }
if (isAlt) { saved_keys += "Alt-"; }
saved_keys += c;
if (isAlt || isCtrl)
{
saved_keys += "]";
}
}
}

Expand Down Expand Up @@ -632,7 +650,7 @@ void SdlWindow::signalKeyDown(SDL_Keycode k, SDL_Keymod m)
queueEvents({ event });

// The same condition as in keyDownEvent().
if ((k >= 32 && k < 127) && (m & (KMOD_CTRL | KMOD_ALT | KMOD_GUI)) == 0)
if ((k >= 32 && k < 127) && (m & (KMOD_CTRL | KMOD_LALT | KMOD_GUI)) == 0)
{
event.type = SDL_TEXTINPUT;
event.text.windowID = window_id;
Expand Down
2 changes: 2 additions & 0 deletions lib/sdl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ class SdlWindow
std::string screenshot_file;
bool screenshot_convert;
bool lastKeyDownProcessed;
Uint16 lastKeyDownMods;
char lastKeyDownChar;

// internal event handlers
void windowEvent(SDL_WindowEvent& ew);
Expand Down

0 comments on commit d27f2ed

Please sign in to comment.