/* -*- mode: c++ -*-
* Kaleidoscope-Escape-OneShot -- Turn ESC into a key that cancels OneShots, if active.
* Copyright (C) 2016-2020 Keyboard.io, Inc
*
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation, version 3.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see .
*/
#include "kaleidoscope/Runtime.h"
#include
#include
#include "kaleidoscope/keyswitch_state.h"
#include "kaleidoscope/layers.h"
namespace kaleidoscope {
namespace plugin {
Key EscapeOneShot::cancel_oneshot_key_{Key_Escape};
EventHandlerResult EscapeOneShot::onKeyEvent(KeyEvent &event) {
// We only act on an escape key (or `cancel_oneshot_key_`, if that has been
// set) that has just been pressed, and not generated by some other
// plugin. Also, only if at least one OneShot key is active and/or
// sticky. Last, only if there are no OneShot keys currently being held.
if (event.key == cancel_oneshot_key_ &&
keyToggledOn(event.state) &&
(event.state & INJECTED) == 0 &&
::OneShot.isActive()) {
// Cancel all OneShot keys
::OneShot.cancel(true);
// Change the cancellation key to a blank key, and signal that event
// processing is complete.
event.key = Key_NoKey;
return EventHandlerResult::EVENT_CONSUMED;
}
// Otherwise, do nothing
return EventHandlerResult::OK;
}
}
}
kaleidoscope::plugin::EscapeOneShot EscapeOneShot;