| 1 |
/**************************************************************************\ |
| 2 |
* Copyright (c) Kongsberg Oil & Gas Technologies AS |
| 3 |
* All rights reserved. |
| 4 |
* |
| 5 |
* Redistribution and use in source and binary forms, with or without |
| 6 |
* modification, are permitted provided that the following conditions are |
| 7 |
* met: |
| 8 |
* |
| 9 |
* Redistributions of source code must retain the above copyright notice, |
| 10 |
* this list of conditions and the following disclaimer. |
| 11 |
* |
| 12 |
* Redistributions in binary form must reproduce the above copyright |
| 13 |
* notice, this list of conditions and the following disclaimer in the |
| 14 |
* documentation and/or other materials provided with the distribution. |
| 15 |
* |
| 16 |
* Neither the name of the copyright holder nor the names of its |
| 17 |
* contributors may be used to endorse or promote products derived from |
| 18 |
* this software without specific prior written permission. |
| 19 |
* |
| 20 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 |
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 |
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 |
* HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 |
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 |
\**************************************************************************/ |
| 32 |
|
| 33 |
#include <Inventor/errors/SoDebugError.h> |
| 34 |
|
| 35 |
#include "KeyboardP.h" |
| 36 |
#include "devices/Keyboard.h" |
| 37 |
|
| 38 |
|
| 39 |
using namespace SIM::Coin3D::Quarter; |
| 40 |
|
| 41 |
#define PUBLIC(obj) obj->publ |
| 42 |
|
| 43 |
KeyboardP::KeyboardP(Keyboard * publ) |
| 44 |
{ |
| 45 |
PUBLIC(this) = publ; |
| 46 |
this->keyboard = new SoKeyboardEvent; |
| 47 |
|
| 48 |
if (!keyboardmap) { |
| 49 |
keyboardmap = new KeyMap; |
| 50 |
keypadmap = new KeyMap; |
| 51 |
this->initKeyMap(); |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
KeyboardP::~KeyboardP() |
| 56 |
{ |
| 57 |
delete this->keyboard; |
| 58 |
} |
| 59 |
|
| 60 |
bool |
| 61 |
KeyboardP::debugKeyEvents() |
| 62 |
{ |
| 63 |
const char * env = coin_getenv("QUARTER_DEBUG_KEYEVENTS"); |
| 64 |
return env && (atoi(env) > 0); |
| 65 |
} |
| 66 |
|
| 67 |
const SoEvent * |
| 68 |
KeyboardP::keyEvent(QKeyEvent * qevent) |
| 69 |
{ |
| 70 |
const Qt::KeyboardModifiers modifiers = qevent->modifiers(); |
| 71 |
|
| 72 |
this->keyboard->setPosition(PUBLIC(this)->mousepos); |
| 73 |
PUBLIC(this)->setModifiers(this->keyboard, qevent); |
| 74 |
|
| 75 |
(qevent->type() == QEvent::KeyPress) ? |
| 76 |
this->keyboard->setState(SoButtonEvent::DOWN): |
| 77 |
this->keyboard->setState(SoButtonEvent::UP); |
| 78 |
|
| 79 |
Qt::Key qkey = (Qt::Key) qevent->key(); |
| 80 |
|
| 81 |
SoKeyboardEvent::Key sokey = (modifiers & Qt::KeypadModifier) ? |
| 82 |
keypadmap->value(qkey, SoKeyboardEvent::ANY) : |
| 83 |
keyboardmap->value(qkey, SoKeyboardEvent::ANY); |
| 84 |
|
| 85 |
//Need to use a temporary to avoid reference becoming deleted before |
| 86 |
//we get a hold of it. |
| 87 |
QByteArray tmp = qevent->text().toLatin1(); |
| 88 |
const char * printable = tmp.constData(); |
| 89 |
this->keyboard->setPrintableCharacter(*printable); |
| 90 |
this->keyboard->setKey(sokey); |
| 91 |
|
| 92 |
#if QUARTER_DEBUG |
| 93 |
if (KeyboardP::debugKeyEvents()) { |
| 94 |
SbString s; |
| 95 |
this->keyboard->enumToString(this->keyboard->getKey(), s); |
| 96 |
SoDebugError::postInfo("KeyboardP::keyEvent", |
| 97 |
"enum: '%s', pos: <%i %i>, printable: '%s'", |
| 98 |
s.getString(), |
| 99 |
PUBLIC(this)->mousepos[0], |
| 100 |
PUBLIC(this)->mousepos[1], |
| 101 |
printable); |
| 102 |
} |
| 103 |
#endif |
| 104 |
return this->keyboard; |
| 105 |
} |
| 106 |
|
| 107 |
KeyboardP::KeyMap * KeyboardP::keyboardmap = nullptr; |
| 108 |
KeyboardP::KeyMap * KeyboardP::keypadmap = nullptr; |
| 109 |
|
| 110 |
void |
| 111 |
KeyboardP::initKeyMap() |
| 112 |
{ |
| 113 |
// keyboard |
| 114 |
keyboardmap->insert(Qt::Key_Shift, SoKeyboardEvent::LEFT_SHIFT); |
| 115 |
keyboardmap->insert(Qt::Key_Alt, SoKeyboardEvent::LEFT_ALT); |
| 116 |
keyboardmap->insert(Qt::Key_Control, SoKeyboardEvent::LEFT_CONTROL); |
| 117 |
keyboardmap->insert(Qt::Key_0, SoKeyboardEvent::NUMBER_0); |
| 118 |
keyboardmap->insert(Qt::Key_1, SoKeyboardEvent::NUMBER_1); |
| 119 |
keyboardmap->insert(Qt::Key_2, SoKeyboardEvent::NUMBER_2); |
| 120 |
keyboardmap->insert(Qt::Key_3, SoKeyboardEvent::NUMBER_3); |
| 121 |
keyboardmap->insert(Qt::Key_4, SoKeyboardEvent::NUMBER_4); |
| 122 |
keyboardmap->insert(Qt::Key_5, SoKeyboardEvent::NUMBER_5); |
| 123 |
keyboardmap->insert(Qt::Key_6, SoKeyboardEvent::NUMBER_6); |
| 124 |
keyboardmap->insert(Qt::Key_7, SoKeyboardEvent::NUMBER_7); |
| 125 |
keyboardmap->insert(Qt::Key_8, SoKeyboardEvent::NUMBER_8); |
| 126 |
keyboardmap->insert(Qt::Key_9, SoKeyboardEvent::NUMBER_9); |
| 127 |
|
| 128 |
keyboardmap->insert(Qt::Key_A, SoKeyboardEvent::A); |
| 129 |
keyboardmap->insert(Qt::Key_B, SoKeyboardEvent::B); |
| 130 |
keyboardmap->insert(Qt::Key_C, SoKeyboardEvent::C); |
| 131 |
keyboardmap->insert(Qt::Key_D, SoKeyboardEvent::D); |
| 132 |
keyboardmap->insert(Qt::Key_E, SoKeyboardEvent::E); |
| 133 |
keyboardmap->insert(Qt::Key_F, SoKeyboardEvent::F); |
| 134 |
keyboardmap->insert(Qt::Key_G, SoKeyboardEvent::G); |
| 135 |
keyboardmap->insert(Qt::Key_H, SoKeyboardEvent::H); |
| 136 |
keyboardmap->insert(Qt::Key_I, SoKeyboardEvent::I); |
| 137 |
keyboardmap->insert(Qt::Key_J, SoKeyboardEvent::J); |
| 138 |
keyboardmap->insert(Qt::Key_K, SoKeyboardEvent::K); |
| 139 |
keyboardmap->insert(Qt::Key_L, SoKeyboardEvent::L); |
| 140 |
keyboardmap->insert(Qt::Key_M, SoKeyboardEvent::M); |
| 141 |
keyboardmap->insert(Qt::Key_N, SoKeyboardEvent::N); |
| 142 |
keyboardmap->insert(Qt::Key_O, SoKeyboardEvent::O); |
| 143 |
keyboardmap->insert(Qt::Key_P, SoKeyboardEvent::P); |
| 144 |
keyboardmap->insert(Qt::Key_Q, SoKeyboardEvent::Q); |
| 145 |
keyboardmap->insert(Qt::Key_R, SoKeyboardEvent::R); |
| 146 |
keyboardmap->insert(Qt::Key_S, SoKeyboardEvent::S); |
| 147 |
keyboardmap->insert(Qt::Key_T, SoKeyboardEvent::T); |
| 148 |
keyboardmap->insert(Qt::Key_U, SoKeyboardEvent::U); |
| 149 |
keyboardmap->insert(Qt::Key_V, SoKeyboardEvent::V); |
| 150 |
keyboardmap->insert(Qt::Key_W, SoKeyboardEvent::W); |
| 151 |
keyboardmap->insert(Qt::Key_X, SoKeyboardEvent::X); |
| 152 |
keyboardmap->insert(Qt::Key_Y, SoKeyboardEvent::Y); |
| 153 |
keyboardmap->insert(Qt::Key_Z, SoKeyboardEvent::Z); |
| 154 |
|
| 155 |
keyboardmap->insert(Qt::Key_Home, SoKeyboardEvent::HOME); |
| 156 |
keyboardmap->insert(Qt::Key_Left, SoKeyboardEvent::LEFT_ARROW); |
| 157 |
keyboardmap->insert(Qt::Key_Up, SoKeyboardEvent::UP_ARROW); |
| 158 |
keyboardmap->insert(Qt::Key_Right, SoKeyboardEvent::RIGHT_ARROW); |
| 159 |
keyboardmap->insert(Qt::Key_Down, SoKeyboardEvent::DOWN_ARROW); |
| 160 |
keyboardmap->insert(Qt::Key_PageUp, SoKeyboardEvent::PAGE_UP); |
| 161 |
keyboardmap->insert(Qt::Key_PageDown, SoKeyboardEvent::PAGE_DOWN); |
| 162 |
keyboardmap->insert(Qt::Key_End, SoKeyboardEvent::END); |
| 163 |
|
| 164 |
keyboardmap->insert(Qt::Key_F1, SoKeyboardEvent::F1); |
| 165 |
keyboardmap->insert(Qt::Key_F2, SoKeyboardEvent::F2); |
| 166 |
keyboardmap->insert(Qt::Key_F3, SoKeyboardEvent::F3); |
| 167 |
keyboardmap->insert(Qt::Key_F4, SoKeyboardEvent::F4); |
| 168 |
keyboardmap->insert(Qt::Key_F5, SoKeyboardEvent::F5); |
| 169 |
keyboardmap->insert(Qt::Key_F6, SoKeyboardEvent::F6); |
| 170 |
keyboardmap->insert(Qt::Key_F7, SoKeyboardEvent::F7); |
| 171 |
keyboardmap->insert(Qt::Key_F8, SoKeyboardEvent::F8); |
| 172 |
keyboardmap->insert(Qt::Key_F9, SoKeyboardEvent::F9); |
| 173 |
keyboardmap->insert(Qt::Key_F10, SoKeyboardEvent::F10); |
| 174 |
keyboardmap->insert(Qt::Key_F11, SoKeyboardEvent::F11); |
| 175 |
keyboardmap->insert(Qt::Key_F12, SoKeyboardEvent::F12); |
| 176 |
|
| 177 |
keyboardmap->insert(Qt::Key_Backspace, SoKeyboardEvent::BACKSPACE); |
| 178 |
keyboardmap->insert(Qt::Key_Tab, SoKeyboardEvent::TAB); |
| 179 |
keyboardmap->insert(Qt::Key_Return, SoKeyboardEvent::RETURN); |
| 180 |
keyboardmap->insert(Qt::Key_Enter, SoKeyboardEvent::ENTER); |
| 181 |
keyboardmap->insert(Qt::Key_Pause, SoKeyboardEvent::PAUSE); |
| 182 |
keyboardmap->insert(Qt::Key_ScrollLock, SoKeyboardEvent::SCROLL_LOCK); |
| 183 |
keyboardmap->insert(Qt::Key_Escape, SoKeyboardEvent::ESCAPE); |
| 184 |
keyboardmap->insert(Qt::Key_Delete, SoKeyboardEvent::DELETE); |
| 185 |
keyboardmap->insert(Qt::Key_Print, SoKeyboardEvent::PRINT); |
| 186 |
keyboardmap->insert(Qt::Key_Insert, SoKeyboardEvent::INSERT); |
| 187 |
keyboardmap->insert(Qt::Key_NumLock, SoKeyboardEvent::NUM_LOCK); |
| 188 |
keyboardmap->insert(Qt::Key_CapsLock, SoKeyboardEvent::CAPS_LOCK); |
| 189 |
|
| 190 |
keyboardmap->insert(Qt::Key_Space, SoKeyboardEvent::SPACE); |
| 191 |
keyboardmap->insert(Qt::Key_Apostrophe, SoKeyboardEvent::APOSTROPHE); |
| 192 |
keyboardmap->insert(Qt::Key_Comma, SoKeyboardEvent::COMMA); |
| 193 |
keyboardmap->insert(Qt::Key_Minus, SoKeyboardEvent::MINUS); |
| 194 |
keyboardmap->insert(Qt::Key_Period, SoKeyboardEvent::PERIOD); |
| 195 |
keyboardmap->insert(Qt::Key_Slash, SoKeyboardEvent::SLASH); |
| 196 |
keyboardmap->insert(Qt::Key_Semicolon, SoKeyboardEvent::SEMICOLON); |
| 197 |
keyboardmap->insert(Qt::Key_Equal, SoKeyboardEvent::EQUAL); |
| 198 |
keyboardmap->insert(Qt::Key_BracketLeft, SoKeyboardEvent::BRACKETLEFT); |
| 199 |
keyboardmap->insert(Qt::Key_BracketRight, SoKeyboardEvent::BRACKETRIGHT); |
| 200 |
keyboardmap->insert(Qt::Key_Backslash, SoKeyboardEvent::BACKSLASH); |
| 201 |
keyboardmap->insert(Qt::Key_Agrave, SoKeyboardEvent::GRAVE); |
| 202 |
|
| 203 |
// keypad |
| 204 |
|
| 205 |
// on Mac OS X, the keypad modifier will also be set when an arrow |
| 206 |
// key is pressed as the arrow keys are considered part of the |
| 207 |
// keypad |
| 208 |
keypadmap->insert(Qt::Key_Left, SoKeyboardEvent::LEFT_ARROW); |
| 209 |
keypadmap->insert(Qt::Key_Up, SoKeyboardEvent::UP_ARROW); |
| 210 |
keypadmap->insert(Qt::Key_Right, SoKeyboardEvent::RIGHT_ARROW); |
| 211 |
keypadmap->insert(Qt::Key_Down, SoKeyboardEvent::DOWN_ARROW); |
| 212 |
|
| 213 |
keypadmap->insert(Qt::Key_Enter, SoKeyboardEvent::PAD_ENTER); |
| 214 |
keypadmap->insert(Qt::Key_F1, SoKeyboardEvent::PAD_F1); |
| 215 |
keypadmap->insert(Qt::Key_F2, SoKeyboardEvent::PAD_F2); |
| 216 |
keypadmap->insert(Qt::Key_F3, SoKeyboardEvent::PAD_F3); |
| 217 |
keypadmap->insert(Qt::Key_F4, SoKeyboardEvent::PAD_F4); |
| 218 |
keypadmap->insert(Qt::Key_0, SoKeyboardEvent::PAD_0); |
| 219 |
keypadmap->insert(Qt::Key_1, SoKeyboardEvent::PAD_1); |
| 220 |
keypadmap->insert(Qt::Key_2, SoKeyboardEvent::PAD_2); |
| 221 |
keypadmap->insert(Qt::Key_3, SoKeyboardEvent::PAD_3); |
| 222 |
keypadmap->insert(Qt::Key_4, SoKeyboardEvent::PAD_4); |
| 223 |
keypadmap->insert(Qt::Key_5, SoKeyboardEvent::PAD_5); |
| 224 |
keypadmap->insert(Qt::Key_6, SoKeyboardEvent::PAD_6); |
| 225 |
keypadmap->insert(Qt::Key_7, SoKeyboardEvent::PAD_7); |
| 226 |
keypadmap->insert(Qt::Key_8, SoKeyboardEvent::PAD_8); |
| 227 |
keypadmap->insert(Qt::Key_9, SoKeyboardEvent::PAD_9); |
| 228 |
keypadmap->insert(Qt::Key_Plus, SoKeyboardEvent::PAD_ADD); |
| 229 |
keypadmap->insert(Qt::Key_Minus, SoKeyboardEvent::PAD_SUBTRACT); |
| 230 |
keypadmap->insert(Qt::Key_multiply, SoKeyboardEvent::PAD_MULTIPLY); |
| 231 |
keypadmap->insert(Qt::Key_division, SoKeyboardEvent::PAD_DIVIDE); |
| 232 |
keypadmap->insert(Qt::Key_Tab, SoKeyboardEvent::PAD_TAB); |
| 233 |
keypadmap->insert(Qt::Key_Space, SoKeyboardEvent::PAD_SPACE); |
| 234 |
keypadmap->insert(Qt::Key_Insert, SoKeyboardEvent::PAD_INSERT); |
| 235 |
keypadmap->insert(Qt::Key_Delete, SoKeyboardEvent::PAD_DELETE); |
| 236 |
keypadmap->insert(Qt::Key_Period, SoKeyboardEvent::PAD_PERIOD); |
| 237 |
|
| 238 |
} |
| 239 |
|
| 240 |
#undef PUBLIC |