Rise
The Vieneo Province
Loading...
Searching...
No Matches
keyboard.cpp
Go to the documentation of this file.
1#include "keyboard.h"
2#include "Viewscreen.h"
3#include "../Dialogs/ConfigurationDialog.h" // @todo this should eventually be unused
4
5
7{
8 viewscreen = ptr;
9 logger = ptr->logger;
10}
11
13{
14 if (g_pKeyboard)
15 g_pKeyboard->Unacquire();
17}
18
20{
21 // no way to configure it so it doesn't ever need to be released
22 if (g_pKeyboard)
23 return S_OK;
24
25 logger->Log("KEYBOARD::Initialize() called...");
26
27 HRESULT hr;
28
29 // Obtain an interface to the system keyboard device.
30 if (FAILED(hr = viewscreen->g_pDI->CreateDevice(GUID_SysKeyboard, &g_pKeyboard, NULL)))
31 {
32 logger->Log("CreateDevice() failed!", Logger::Level::Warn);
33 return hr;
34 }
35
36 // Set the data format to "keyboard format" - a predefined data format
37 //
38 // A data format specifies which controls on a device we
39 // are interested in, and how they should be reported.
40 //
41 // This tells DirectInput that we will be passing an array
42 // of 256 bytes to IDirectInputDevice::GetDeviceState.
43 if (FAILED(hr = g_pKeyboard->SetDataFormat(&c_dfDIKeyboard)))
44 {
45 if (hr == DIERR_ACQUIRED)
46 logger->Log("SetDataFormat() failed: DIERR_ACQUIRED", Logger::Level::Warn); // The operation cannot be performed while the device is acquired.
47 else if (hr == DIERR_INVALIDPARAM)
48 logger->Log("SetDataFormat() failed: DIERR_INVALIDPARAM", Logger::Level::Warn); // An invalid parameter was passed to the returning function, or the object was not in a state that permitted the function to be called. This value is equal to the E_INVALIDARG standard COM return value.
49 else if (hr == DIERR_NOTINITIALIZED)
50 logger->Log("SetDataFormat() failed: DIERR_NOTINITIALIZED", Logger::Level::Warn); // This object has not been initialized.
51 else
52 logger->Log("SetDataFormat() failed: DIERR_UNKNOWN", Logger::Level::Warn);
53 return hr;
54 }
55
56 // Set the cooperative level to let DirectInput know how
57 // this device should interact with the system and with other
58 // DirectInput applications.
59 if (FAILED(hr = g_pKeyboard->SetCooperativeLevel(hDlg, DISCL_NONEXCLUSIVE | DISCL_FOREGROUND | DISCL_NOWINKEY)))
60 {
61 logger->Log("SetCooperativeLevel() failed!", Logger::Level::Warn);
62 return hr;
63 }
64
65 return S_OK;
66}
67
68void keyboard::Update(float fElapsedTime)
69{
71 const auto cancelInstrumentRepeat = [this]()
72 {
73 viewscreen->gameclass->displays->UpdateKeyRepeat(nullptr, false, 0.0f);
74 };
75
77 SetScrollLock(fmodf(static_cast<float>(viewscreen->gameclass->bus->ProgramTime), 2.0f) > 1.0f);
78 else
79 SetScrollLock(false);
80
82 {
83 cancelInstrumentRepeat();
84 return;
85 }
86
87 // Get the input's device state, and put the state in dims
88 unsigned char diks[256]; // DirectInput keyboard state buffer
89 if (FAILED(g_pKeyboard->GetDeviceState(256, diks)))
90 {
91 cancelInstrumentRepeat();
92 g_pKeyboard->Acquire();
93 return;
94 }
95
96 // Read the string of the index values of the keys that are down
97 if (dead && !playerships[0].isSimulator)
98 {
99 cancelInstrumentRepeat();
100 return;
101 }
102
103 g_bAlt = diks[DIK_LALT] || diks[DIK_RALT];
104 if (g_bAlt)
105 {
106 cancelInstrumentRepeat();
107 return;
108 }
109
110 if (g_bTextInput == 1)
111 {
113 ptr->SetKey(0);
114 }
115
116 frameTime = 0.2f * fElapsedTime;
117
118 g_bShift = diks[DIK_LSHIFT] || diks[DIK_RSHIFT];
119 g_bControl = diks[DIK_LCONTROL] || diks[DIK_RCONTROL];
120
121 bool handled = false;
122 for (short t = DIK_ESCAPE; t <= DIK_DELETE; t++)
123 {
124 if (t == DIK_LSHIFT || t == DIK_RSHIFT || t == DIK_LCONTROL || t == DIK_RCONTROL || t == DIK_LALT || t == DIK_RALT) // augmenting keys
125 continue;
126
127 if (!(diks[t] & 0x80)) // not depressed
128 continue;
129
130 // let the display system have first dibs
132 {
133 handled = true;
134 break;
135 }
136
137 if (g_bTextInput == 1)
138 {
140 ptr->SetKey(t);
141 handled = true;
142 }
143
144 // ReSharper disable once CppDefaultCaseNotHandledInSwitchStatement
145 switch (t)
146 {
147 //if (g_bControl && viewscreen->gameclass->GUI->IsAdmin && viewscreen->camera->outside)
148 //{
149 // viewscreen->camera->position.z += 0.0001f;
150 //}
151
152 // reserved copy to clipboard
153 case DIK_C:
154 case DIK_INSERT:
155 if (!olddiks[t] && g_bControl)
156 {
157 if (DialogBase::dialogs[D_COMMUNICATIONS]->linecursel != -1)
158 {
159 const int chatline = DialogBase::dialogs[D_COMMUNICATIONS]->linecursel;
160 const wchar_t* str = viewscreen->gameclass->GUI->chatchannel[DialogBase::dialogs[D_COMMUNICATIONS]->activetab].formatchatline.at(chatline).c_str();
161
162 const HGLOBAL hMem = GlobalAlloc(GMEM_MOVEABLE, wcslen(str) * 2 + 2);
163 memcpy(GlobalLock(hMem), str, wcslen(str) * 2 + 2);
164 GlobalUnlock(hMem);
165
166 OpenClipboard(nullptr);
167 EmptyClipboard();
168 SetClipboardData(CF_UNICODETEXT, hMem);
169 CloseClipboard();
170
173 }
174 }
175 break;
176
177 //if (g_bControl && viewscreen->gameclass->GUI->IsAdmin && viewscreen->camera->outside)
178 //{
179 // viewscreen->camera->position.z -= 0.0001f;
180 //}
181
182 //case DIK_T: // special task ticketing
183 //{
184 // if (!olddiks[t] && !g_bShift)
185 // {
186 // if (playerships[0].police)
187 // {
188 // if (!playerships[0].specialight)
189 // {
190 // viewscreen->gameclass->networking->RequestVerbiage(ActiveHelp::FirstTurnOnLightBar);
191 // }
192 // else if (targetC >= 0)
193 // {
194 // SClientPacket outpacket;
195 // outpacket.type = 26; // ticketing
196 // outpacket.f_x = 0.0f; // NULL
197 // outpacket.f_y = 0.0f; // NULL
198 // outpacket.f_z = 0.0f; // NULL
199 // outpacket.f_w = static_cast<float>(playerships[viewscreen->scanslot[targetC]].vehicleId);
200 // viewscreen->gameclass->networking->SendToServer(&outpacket, sizeof(SClientPacket), true);
201 // }
202 // else
203 // {
204 // viewscreen->gameclass->networking->RequestVerbiage(ActiveHelp::FirstSelectVehicleOrContainer);
205 // }
206 // }
207 // }
208 //}
209 //break;
210
211 // Summon the beast!
212 //case DIK_U:
213 // if (g_bShift && g_bControl && viewscreen->gameclass->GUI->IsAdmin)
214 // {
215 // playerships[MAX_SCAN - 1].logolight = false;
216 // playerships[MAX_SCAN - 1].active = true;
217 // playerships[MAX_SCAN - 1].visible = true;
218 // playerships[MAX_SCAN - 1].isOnScanner = true;
219 // //playerships[MAX_SCAN - 1].clientinit = true;
220 // playerships[MAX_SCAN - 1].reference = REF_ONGROUND;
221 // playerships[MAX_SCAN - 1].align = 1;
222 // playerships[MAX_SCAN - 1].type = BeishtKione;
223 // viewscreen->LoadVehicleProfile(MAX_SCAN - 1);
224 // playerships[MAX_SCAN - 1].acc = playerships[MAX_SCAN - 1].acciter = playerships[MAX_SCAN - 1].accleft = centerC;
225 // playerships[MAX_SCAN - 1].positer = playerships[MAX_SCAN - 1].posleft = centerC;
226 // playerships[MAX_SCAN - 1].position = RiseMath::Vector3(-1274.755f, -2639.425f, 226.8252f);
227 // playerships[MAX_SCAN - 1].precisionx = static_cast<double>(playerships[MAX_SCAN - 1].position.x);
228 // playerships[MAX_SCAN - 1].precisiony = static_cast<double>(playerships[MAX_SCAN - 1].position.y);
229 // playerships[MAX_SCAN - 1].precisionz = static_cast<double>(playerships[MAX_SCAN - 1].position.z);
230 // RiseMath::Vector3 gthrust;
231 // float radius;
232 // playerships[MAX_SCAN - 1].velocity = playerships[MAX_SCAN - 1].barycentric = Viewscreen::CalculateBarycentric(&playerships[MAX_SCAN - 1].position, &gthrust, &radius);
233 // playerships[MAX_SCAN - 1].veliter = playerships[MAX_SCAN - 1].velleft = centerC;
234 // playerships[MAX_SCAN - 1].orientation = RiseMath::Quaternion(-0.3331781f, 0.1119807f, 0.9148517f, -0.1987435f);
235 // RiseMath::QuaternionIdentity(&playerships[MAX_SCAN - 1].orientiter);
236 // RiseMath::QuaternionIdentity(&playerships[MAX_SCAN - 1].orientleft);
237 // }
238 // break;
239
240 // was ctrl+A
241 //if (gameclass->GUI->IsAdmin && camera->outside)
242 // camera->position.x -= 0.0001f;
243
244 // was ctrl+D
245 //if (gameclass->GUI->IsAdmin && camera->outside)
246 // camera->position.x += 0.0001f;
247
248 }
249 }
250
251 // Only an instrument that accepted a navigation press may repeat it. Do not
252 // count the press frame toward the initial delay, or repeat during text entry.
253 const bool repeatEnabled = !g_bTextInput && ourcockpit.power > 0.5f;
255 repeatEnabled ? diks : nullptr, g_bShift, handled ? 0.0f : fElapsedTime))
256 handled = true;
257
258 if (!handled)
259 {
260 char profile = ProfileFlags::GLOBAL;
262 profile |= ProfileFlags::INTERIOR;
263 else if (playerships[0].groundvehicle)
264 profile |= ProfileFlags::GV;
265 else
266 profile |= ProfileFlags::AIRSPACE;
267 for (auto binding : viewscreen->gameclass->config.keyBindings)
268 {
269 if (allowedUsage[binding.function] & profile && (diks[binding.scancode] & 0x80) && binding.shift == g_bShift)
270 viewscreen->RunBindingFunction(binding.function, olddiks[binding.scancode]);
271 }
272 }
273
274 memcpy_s(olddiks, 256, diks, 256);
275
277 {
278 viewscreen->lateralincr += 0.1f * fElapsedTime;
279 if (viewscreen->lateralincr > 1.0f)
280 viewscreen->lateralincr = 1.0f;
281 }
282 else
283 viewscreen->lateralincr = 0.1f;
285 {
286 viewscreen->verticalincr += 0.1f * fElapsedTime;
287 if (viewscreen->verticalincr > 1.0f)
288 viewscreen->verticalincr = 1.0f;
289 }
290 else
291 viewscreen->verticalincr = 0.1f;
292}
293
294void keyboard::SetScrollLock(bool setState)
295{
296 const bool curState = LOWORD(GetKeyState(VK_SCROLL)) != 0;
297 if (curState != setState)
298 {
299 // Simulate a key press
300 keybd_event(VK_SCROLL, DIK_SCROLL, KEYEVENTF_EXTENDEDKEY | 0, 0);
301
302 // Simulate a key release
303 keybd_event(VK_SCROLL, DIK_SCROLL, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
304 }
305}
306
307std::string keyboard::Translate(short scancode)
308{
309 switch (scancode)
310 {
311 case DIK_ESCAPE: return "ESC";
312 case DIK_1: return "1";
313 case DIK_2: return "2";
314 case DIK_3: return "3";
315 case DIK_4: return "4";
316 case DIK_5: return "5";
317 case DIK_6: return "6";
318 case DIK_7: return "7";
319 case DIK_8: return "8";
320 case DIK_9: return "9";
321 case DIK_0: return "0";
322 case DIK_MINUS: return "-";
323 case DIK_EQUALS: return "=";
324 case DIK_BACK: return "BACK";
325 case DIK_TAB: return "TAB";
326 case DIK_Q: return "Q";
327 case DIK_W: return "W";
328 case DIK_E: return "E";
329 case DIK_R: return "R";
330 case DIK_T: return "T";
331 case DIK_Y: return "Y";
332 case DIK_U: return "U";
333 case DIK_I: return "I";
334 case DIK_O: return "O";
335 case DIK_P: return "P";
336 case DIK_LBRACKET: return "[";
337 case DIK_RBRACKET: return "]";
338 case DIK_RETURN: return "ENTER";
339 case DIK_A: return "A";
340 case DIK_S: return "S";
341 case DIK_D: return "D";
342 case DIK_F: return "F";
343 case DIK_G: return "G";
344 case DIK_H: return "H";
345 case DIK_J: return "J";
346 case DIK_K: return "K";
347 case DIK_L: return "L";
348 case DIK_SEMICOLON: return ";";
349 case DIK_APOSTROPHE: return "'";
350 case DIK_GRAVE: return "~";
351 case DIK_BACKSLASH: return "\\";
352 case DIK_Z: return "Z";
353 case DIK_X: return "X";
354 case DIK_C: return "C";
355 case DIK_V: return "V";
356 case DIK_B: return "B";
357 case DIK_N: return "N";
358 case DIK_M: return "M";
359 case DIK_COMMA: return ",";
360 case DIK_PERIOD: return ".";
361 case DIK_SLASH: return "/";
362 case DIK_MULTIPLY: return "NUM*";
363 case DIK_SPACE: return "SPACE";
364 case DIK_CAPITAL: return "";
365 case DIK_F1: return "F1";
366 case DIK_F2: return "F2";
367 case DIK_F3: return "F3";
368 case DIK_F4: return "F4";
369 case DIK_F5: return "F5";
370 case DIK_F6: return "F6";
371 case DIK_F7: return "F7";
372 case DIK_F8: return "F8";
373 case DIK_F9: return "F9";
374 case DIK_F10: return "F10";
375 case DIK_NUMLOCK: return "NUMLOCK";
376 case DIK_SCROLL: return "SCROLL";
377 case DIK_NUMPAD7: return "NUM7";
378 case DIK_NUMPAD8: return "NUM8";
379 case DIK_NUMPAD9: return "NUM9";
380 case DIK_SUBTRACT: return "NUM-";
381 case DIK_NUMPAD4: return "NUM4";
382 case DIK_NUMPAD5: return "NUM5";
383 case DIK_NUMPAD6: return "NUM6";
384 case DIK_ADD: return "NUM+";
385 case DIK_NUMPAD1: return "NUM1";
386 case DIK_NUMPAD2: return "NUM2";
387 case DIK_NUMPAD3: return "NUM3";
388 case DIK_NUMPAD0: return "NUM0";
389 case DIK_DECIMAL: return "NUM.";
390 //case DIK_OEM_102: return "";
391 case DIK_F11: return "F11";
392 case DIK_F12: return "F12";
393 //case DIK_F13: return "F13";
394 //case DIK_F14: return "F14";
395 //case DIK_F15: return "F15";
396 //case DIK_KANA: return "";
397 //case DIK_ABNT_C1: return "";
398 //case DIK_CONVERT: return "";
399 //case DIK_NOCONVERT: return "";
400 //case DIK_YEN: return "";
401 //case DIK_ABNT_C2: return "";
402 //case DIK_NUMPADEQUALS: return "NUM=";
403 //case DIK_PREVTRACK: return "";
404 //case DIK_AT: return "";
405 //case DIK_COLON: return "";
406 //case DIK_UNDERLINE: return "";
407 //case DIK_KANJI: return "";
408 //case DIK_STOP: return "";
409 //case DIK_AX: return "";
410 //case DIK_UNLABELED: return "";
411 //case DIK_NEXTTRACK: return "";
412 case DIK_NUMPADENTER: return "NUM ENTER";
413 //case DIK_MUTE: return "";
414 //case DIK_CALCULATOR: return "";
415 //case DIK_PLAYPAUSE: return "";
416 //case DIK_MEDIASTOP: return "";
417 //case DIK_VOLUMEDOWN: return "";
418 //case DIK_VOLUMEUP: return "";
419 //case DIK_WEBHOME: return "";
420 case DIK_NUMPADCOMMA: return "NUM,";
421 case DIK_DIVIDE: return "NUM\\";
422 //case DIK_SYSRQ: return "";
423 case DIK_PAUSE: return "PAUSE";
424 case DIK_HOME: return "HOME";
425 case DIK_UP: return "UP";
426 case DIK_PRIOR: return "PgUp";
427 case DIK_LEFT: return "LEFT";
428 case DIK_RIGHT: return "RIGHT";
429 case DIK_END: return "END";
430 case DIK_DOWN: return "DOWN";
431 case DIK_NEXT: return "PgDn";
432 case DIK_INSERT: return "INS";
433 case DIK_DELETE: return "DEL";
434 default: return "Unknown";
435 }
436}
#define SAFE_RELEASE(value)
HRESULT(WINAPI *CUniBuffer::_ScriptApplyDigitSubstitution)(const SCRIPT_DIGITSUBSTITUTE *
@ D_COMMUNICATIONS
Definition DialogBase.h:12
@ D_CONFIGURATION
Definition DialogBase.h:11
@ REF_INANOTHER
Definition ServerTypes.h:45
@ AIRSPACE
Definition binding.h:114
@ GV
Definition binding.h:113
@ INTERIOR
Definition binding.h:112
@ GLOBAL
Definition binding.h:111
bool MasterWarning
Definition Bus.h:171
bool MasterCaution
Definition Bus.h:476
double ProgramTime
Definition Bus.h:473
std::vector< std::wstring > formatchatline
Definition chat.h:61
std::vector< KeyBinding > keyBindings
Definition config.h:54
void SetKey(short scancode)
short activetab
Definition DialogBase.h:64
static DialogBase * dialogs[D_ENUMERATION]
Definition DialogBase.h:41
virtual void UpdateMenu(short tab)
Definition DialogBase.h:114
int linecursel
Definition DialogBase.h:75
bool UpdateKeyRepeat(const unsigned char *keys, bool shift, float elapsed)
Definition Displays.cpp:714
bool HandleKeyPress(short key, bool shift)
Definition Displays.cpp:703
std::unique_ptr< Bus > bus
Definition GameClass.h:108
Config config
Definition GameClass.h:100
std::unique_ptr< HMI > GUI
Definition GameClass.h:106
std::unique_ptr< Displays > displays
Definition GameClass.h:110
bool showKeymapOverlay
Definition GameClass.h:133
SPlayerData player[MAX_ONLINEPLAYERS]
Definition gui.h:1093
bool Editing
Definition gui.h:1109
unsigned char ourplayerC
Definition gui.h:1094
bool Edited
Definition gui.h:1109
CHATCHANNEL chatchannel[MAX_TAB]
Definition gui.h:1096
void Log(const char *msg, Level level=Info, int errorCode=0)
Definition Logger.cpp:283
@ Warn
Definition Logger.h:26
void RunBindingFunction(BindingEnum function, bool repeating)
GameClass * gameclass
Definition Viewscreen.h:439
bool lateralmod
Definition Viewscreen.h:379
float verticalincr
Definition Viewscreen.h:387
LPDIRECTINPUT8 g_pDI
Definition Viewscreen.h:210
bool verticalmod
Definition Viewscreen.h:379
float lateralincr
Definition Viewscreen.h:386
Logger * logger
Definition Viewscreen.h:440
void Update(float fElapsedTime)
Definition keyboard.cpp:68
unsigned char olddiks[256]
Definition keyboard.h:22
bool g_bAlt
Definition keyboard.h:24
std::string Translate(short scancode)
Definition keyboard.cpp:307
float frameTime
Definition keyboard.h:29
bool g_bShift
Definition keyboard.h:31
HRESULT Initialize(HWND hDlg)
Definition keyboard.cpp:19
LPDIRECTINPUTDEVICE8 g_pKeyboard
Definition keyboard.h:21
static void SetScrollLock(bool setState)
Definition keyboard.cpp:294
bool g_bControl
Definition keyboard.h:32
Logger * logger
Definition keyboard.h:19
Viewscreen * viewscreen
Definition keyboard.h:18
keyboard(Viewscreen *ptr)
Definition keyboard.cpp:6
DeathType dead
Definition globals.cpp:37
s_network_objects playerships[MAX_SCAN]
Definition globals.cpp:112
Scockpit ourcockpit
Definition globals.cpp:114
char g_bTextInput
Definition globals.cpp:61
float power
Definition globals.h:599