Skip to content

Commit 789aeb0

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

File tree

5 files changed

+60
-1
lines changed

5 files changed

+60
-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: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333

3434
#define ENABLE_RAW_MOUSE_INPUT 0x01
3535
#define ENABLE_RAW_KEYBOARD_INPUT 0x02
36+
#define RAW_KEYBOARD_FLAG_NOHOTKEYS 0x04
3637

3738
typedef struct
3839
{
@@ -78,6 +79,9 @@ static DWORD WINAPI WIN_RawInputThread(LPVOID param)
7879
devices[count].usUsagePage = USB_USAGEPAGE_GENERIC_DESKTOP;
7980
devices[count].usUsage = USB_USAGE_GENERIC_KEYBOARD;
8081
devices[count].dwFlags = 0;
82+
if (data->flags & RAW_KEYBOARD_FLAG_NOHOTKEYS) {
83+
devices[count].dwFlags |= RIDEV_NOHOTKEYS;
84+
}
8185
devices[count].hwndTarget = window;
8286
++count;
8387
}
@@ -198,6 +202,9 @@ static bool WIN_UpdateRawInputEnabled(SDL_VideoDevice *_this)
198202
}
199203
if (data->raw_keyboard_enabled) {
200204
flags |= ENABLE_RAW_KEYBOARD_INPUT;
205+
if (data->raw_keyboard_flag_nohotkeys) {
206+
flags |= RAW_KEYBOARD_FLAG_NOHOTKEYS;
207+
}
201208
}
202209
if (flags != data->raw_input_enabled) {
203210
if (WIN_SetRawInputEnabled(_this, flags)) {
@@ -245,6 +252,25 @@ bool WIN_SetRawKeyboardEnabled(SDL_VideoDevice *_this, bool enabled)
245252
return true;
246253
}
247254

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

250276
bool WIN_SetRawMouseEnabled(SDL_VideoDevice *_this, bool enabled)
@@ -257,6 +283,11 @@ bool WIN_SetRawKeyboardEnabled(SDL_VideoDevice *_this, bool enabled)
257283
return SDL_Unsupported();
258284
}
259285

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

262293
#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)