Skip to content

Commit dfe5c04

Browse files
committed
Add hint for blocking win key when using raw keyboard
1 parent 510c7ed commit dfe5c04

File tree

5 files changed

+61
-1
lines changed

5 files changed

+61
-1
lines changed

include/SDL3/SDL_hints.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4128,6 +4128,20 @@ extern "C" {
41284128
*/
41294129
#define SDL_HINT_WINDOWS_RAW_KEYBOARD "SDL_WINDOWS_RAW_KEYBOARD"
41304130

4131+
/**
4132+
* A variable controlling whether to block hotkeys when raw keyboard events are enabled.
4133+
*
4134+
* The variable can be set to the following values:
4135+
*
4136+
* - "0": Hotkeys are not blocked. (default)
4137+
* - "1": Hotkeys are blocked.
4138+
*
4139+
* This hint can be set anytime.
4140+
*
4141+
* \since This hint is available since SDL 3.2.22.
4142+
*/
4143+
#define SDL_HINT_WINDOWS_RAW_KEYBOARD_NOHOTKEYS "SDL_WINDOWS_RAW_KEYBOARD_NOHOTKEYS"
4144+
41314145
/**
41324146
* A variable controlling whether SDL uses Kernel Semaphores on Windows.
41334147
*

src/video/windows/SDL_windowsrawinput.c

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#if defined(SDL_VIDEO_DRIVER_WINDOWS)
2424

2525
#include "SDL_windowsvideo.h"
26+
#include "SDL_windowsrawinput.h"
2627

2728
#if !defined(SDL_PLATFORM_XBOXONE) && !defined(SDL_PLATFORM_XBOXSERIES)
2829

@@ -33,6 +34,7 @@
3334

3435
#define ENABLE_RAW_MOUSE_INPUT 0x01
3536
#define ENABLE_RAW_KEYBOARD_INPUT 0x02
37+
#define RAW_KEYBOARD_FLAG_NOHOTKEYS 0x04
3638

3739
typedef struct
3840
{
@@ -78,6 +80,9 @@ static DWORD WINAPI WIN_RawInputThread(LPVOID param)
7880
devices[count].usUsagePage = USB_USAGEPAGE_GENERIC_DESKTOP;
7981
devices[count].usUsage = USB_USAGE_GENERIC_KEYBOARD;
8082
devices[count].dwFlags = 0;
83+
if (data->flags & RAW_KEYBOARD_FLAG_NOHOTKEYS) {
84+
devices[count].dwFlags |= RIDEV_NOHOTKEYS;
85+
}
8186
devices[count].hwndTarget = window;
8287
++count;
8388
}
@@ -198,6 +203,9 @@ static bool WIN_UpdateRawInputEnabled(SDL_VideoDevice *_this)
198203
}
199204
if (data->raw_keyboard_enabled) {
200205
flags |= ENABLE_RAW_KEYBOARD_INPUT;
206+
if (data->raw_keyboard_flag_nohotkeys) {
207+
flags |= RAW_KEYBOARD_FLAG_NOHOTKEYS;
208+
}
201209
}
202210
if (flags != data->raw_input_enabled) {
203211
if (WIN_SetRawInputEnabled(_this, flags)) {
@@ -245,6 +253,25 @@ bool WIN_SetRawKeyboardEnabled(SDL_VideoDevice *_this, bool enabled)
245253
return true;
246254
}
247255

256+
bool WIN_SetRawKeyboardFlag(SDL_VideoDevice *_this, WIN_RawKeyboardFlag flag, bool enabled)
257+
{
258+
SDL_VideoData *data = _this->internal;
259+
if (data->gameinput_context) {
260+
return false;
261+
}
262+
263+
switch(flag) {
264+
case NOHOTKEYS:
265+
data->raw_keyboard_flag_nohotkeys = enabled;
266+
break;
267+
default:
268+
return false;
269+
break;
270+
}
271+
272+
return WIN_UpdateRawInputEnabled(_this);
273+
}
274+
248275
#else
249276

250277
bool WIN_SetRawMouseEnabled(SDL_VideoDevice *_this, bool enabled)
@@ -257,6 +284,11 @@ bool WIN_SetRawKeyboardEnabled(SDL_VideoDevice *_this, bool enabled)
257284
return SDL_Unsupported();
258285
}
259286

287+
bool WIN_SetRawKeyboardFlag(SDL_VideoDevice *_this, WIN_RawKeyboardFlag flag, bool enabled)
288+
{
289+
return SDL_Unsupported();
290+
}
291+
260292
#endif // !SDL_PLATFORM_XBOXONE && !SDL_PLATFORM_XBOXSERIES
261293

262294
#endif // SDL_VIDEO_DRIVER_WINDOWS

src/video/windows/SDL_windowsrawinput.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@
2525

2626
extern bool WIN_SetRawMouseEnabled(SDL_VideoDevice *_this, bool enabled);
2727
extern bool WIN_SetRawKeyboardEnabled(SDL_VideoDevice *_this, bool enabled);
28-
extern bool WIN_RefreshRawInputEnabled(SDL_VideoDevice *_this);
28+
29+
typedef enum WIN_RawKeyboardFlag {
30+
NOHOTKEYS
31+
} WIN_RawKeyboardFlag;
32+
extern bool WIN_SetRawKeyboardFlag(SDL_VideoDevice *_this, WIN_RawKeyboardFlag flag, bool enabled);
2933

3034
#endif // SDL_windowsrawinput_h_

src/video/windows/SDL_windowsvideo.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,13 @@ static void SDLCALL UpdateWindowsRawKeyboard(void *userdata, const char *name, c
6262
WIN_SetRawKeyboardEnabled(_this, enabled);
6363
}
6464

65+
static void SDLCALL UpdateWindowsRawKeyboardNoHotkeys(void *userdata, const char *name, const char *oldValue, const char *newValue)
66+
{
67+
SDL_VideoDevice *_this = (SDL_VideoDevice *)userdata;
68+
bool enabled = SDL_GetStringBoolean(newValue, false);
69+
WIN_SetRawKeyboardFlag(_this, NOHOTKEYS, enabled);
70+
}
71+
6572
static void SDLCALL UpdateWindowsEnableMessageLoop(void *userdata, const char *name, const char *oldValue, const char *newValue)
6673
{
6774
g_WindowsEnableMessageLoop = SDL_GetStringBoolean(newValue, true);
@@ -550,6 +557,7 @@ static bool WIN_VideoInit(SDL_VideoDevice *_this)
550557
#endif
551558

552559
SDL_AddHintCallback(SDL_HINT_WINDOWS_RAW_KEYBOARD, UpdateWindowsRawKeyboard, _this);
560+
SDL_AddHintCallback(SDL_HINT_WINDOWS_RAW_KEYBOARD_NOHOTKEYS, UpdateWindowsRawKeyboardNoHotkeys, _this);
553561
SDL_AddHintCallback(SDL_HINT_WINDOWS_ENABLE_MESSAGELOOP, UpdateWindowsEnableMessageLoop, NULL);
554562
SDL_AddHintCallback(SDL_HINT_WINDOWS_ENABLE_MENU_MNEMONICS, UpdateWindowsEnableMenuMnemonics, NULL);
555563
SDL_AddHintCallback(SDL_HINT_WINDOW_FRAME_USABLE_WHILE_CURSOR_HIDDEN, UpdateWindowFrameUsableWhileCursorHidden, NULL);
@@ -569,6 +577,7 @@ void WIN_VideoQuit(SDL_VideoDevice *_this)
569577
SDL_VideoData *data = _this->internal;
570578

571579
SDL_RemoveHintCallback(SDL_HINT_WINDOWS_RAW_KEYBOARD, UpdateWindowsRawKeyboard, _this);
580+
SDL_RemoveHintCallback(SDL_HINT_WINDOWS_RAW_KEYBOARD_NOHOTKEYS, UpdateWindowsRawKeyboardNoHotkeys, _this);
572581
SDL_RemoveHintCallback(SDL_HINT_WINDOWS_ENABLE_MESSAGELOOP, UpdateWindowsEnableMessageLoop, NULL);
573582
SDL_RemoveHintCallback(SDL_HINT_WINDOWS_ENABLE_MENU_MNEMONICS, UpdateWindowsEnableMenuMnemonics, NULL);
574583
SDL_RemoveHintCallback(SDL_HINT_WINDOW_FRAME_USABLE_WHILE_CURSOR_HIDDEN, UpdateWindowFrameUsableWhileCursorHidden, NULL);

src/video/windows/SDL_windowsvideo.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,7 @@ struct SDL_VideoData
486486
SDL_Point last_raw_mouse_position;
487487
bool raw_mouse_enabled;
488488
bool raw_keyboard_enabled;
489+
bool raw_keyboard_flag_nohotkeys;
489490
bool pending_E1_key_sequence;
490491
Uint32 raw_input_enabled;
491492

0 commit comments

Comments
 (0)