Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit9eb73a3

Browse files
committed
Add support for keyboard grab without mouse grab (expose SDL_GetWindowKeyboardGrab)
1 parent26f3a41 commit9eb73a3

File tree

6 files changed

+103
-0
lines changed

6 files changed

+103
-0
lines changed

‎buildconfig/stubs/pygame/event.pyi‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ def set_allowed(type: Optional[_EventTypes]) -> None: ...
4343
defget_blocked(type:_EventTypes)->bool: ...
4444
defset_grab(grab:bool)->None: ...
4545
defget_grab()->bool: ...
46+
defset_keyboard_grab(grab:bool)->None: ...
47+
defget_keyboard_grab()->bool: ...
4648
defpost(event:Event)->bool: ...
4749
defcustom_type()->int: ...
4850

‎docs/reST/ref/event.rst‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,30 @@ On Android, the following events can be generated
427427

428428
.. ## pygame.event.get_grab ##
429429
430+
..function::set_keyboard_grab
431+
432+
|:sl:`grab enables capture of system keyboard shortcuts like Alt+Tab or the Meta/Super key.`
433+
|:sg:`set_keyboard_grab(bool) -> None`
434+
435+
Keyboard grab enables capture of system keyboard shortcuts like Alt+Tab or the Meta/Super key.
436+
Note that not all system keyboard shortcuts can be captured by applications (one example is Ctrl+Alt+Del on Windows).
437+
This is primarily intended for specialized applications such as VNC clients or VM frontends. Normal games should not use keyboard grab.
438+
439+
..versionadded::2.5.0
440+
441+
.. ## pygame.event.set_keyboard_grab ##
442+
443+
..function::get_keyboard_grab
444+
445+
|:sl:`get the current keyboard grab state`
446+
|:sg:`get_keyboard_grab() -> bool`
447+
448+
Returns ``True`` when keyboard grab is enabled.
449+
450+
..versionadded::2.5.0
451+
452+
.. ## pygame.event.get_keyboard_grab ##
453+
430454
..function::post
431455

432456
|:sl:`place a new event on the queue`

‎src_c/cython/pygame/_sdl2/video.pxd‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,13 +243,18 @@ cdef extern from "SDL.h" nogil:
243243
# https://wiki.libsdl.org/SDL_GetGrabbedWindow
244244
# https://wiki.libsdl.org/SDL_GetWindowGrab
245245
# https://wiki.libsdl.org/SDL_SetWindowGrab
246+
# https://wiki.libsdl.org/SDL_GetWindowKeyboardGrab
247+
# https://wiki.libsdl.org/SDL_SetWindowKeyboardGrab
246248
# https://wiki.libsdl.org/SDL_SetWindowFullscreen
247249
# https://wiki.libsdl.org/SDL_SetWindowModalFor
248250
int SDL_GetWindowDisplayIndex(SDL_Window* window)
249251
SDL_Window* SDL_GetGrabbedWindow()
250252
SDL_bool SDL_GetWindowGrab(SDL_Window* window)
251253
void SDL_SetWindowGrab(SDL_Window* window,
252254
SDL_bool grabbed)
255+
SDL_bool SDL_GetWindowKeyboardGrab(SDL_Window* window)
256+
void SDL_SetWindowKeyboardGrab(SDL_Window* window,
257+
SDL_bool grabbed)
253258
int SDL_SetWindowFullscreen(SDL_Window* window,
254259
Uint32 flags)
255260
int SDL_SetWindowModalFor(SDL_Window* modal_window,

‎src_c/doc/event_doc.h‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
#defineDOC_PYGAMEEVENTGETBLOCKED "get_blocked(type) -> bool\nget_blocked(typelist) -> bool\ntest if a type of event is blocked from the queue"
1313
#defineDOC_PYGAMEEVENTSETGRAB "set_grab(bool) -> None\ncontrol the sharing of input devices with other applications"
1414
#defineDOC_PYGAMEEVENTGETGRAB "get_grab() -> bool\ntest if the program is sharing input devices"
15+
#defineDOC_PYGAMEEVENTSETKEYBOARDGRAB "set_keyboard_grab(bool) -> None\ngrab enables capture of system keyboard shortcuts like Alt+Tab or the Meta/Super key."
16+
#defineDOC_PYGAMEEVENTGETKEYBOARDGRAB "get_keyboard_grab() -> bool\nget the current keyboard grab state"
1517
#defineDOC_PYGAMEEVENTPOST "post(Event) -> bool\nplace a new event on the queue"
1618
#defineDOC_PYGAMEEVENTCUSTOMTYPE "custom_type() -> int\nmake custom user event type"
1719
#defineDOC_PYGAMEEVENTEVENT "Event(type, dict) -> Event\nEvent(type, **attributes) -> Event\npygame object for representing events"
@@ -84,6 +86,14 @@ pygame.event.get_grab
8486
get_grab() -> bool
8587
test if the program is sharing input devices
8688
89+
pygame.event.set_keyboard_grab
90+
set_keyboard_grab(bool) -> None
91+
grab enables capture of system keyboard shortcuts like Alt+Tab or the Meta/Super key.
92+
93+
pygame.event.get_keyboard_grab
94+
get_keyboard_grab() -> bool
95+
get the current keyboard grab state
96+
8797
pygame.event.post
8898
post(Event) -> bool
8999
place a new event on the queue

‎src_c/event.c‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1591,6 +1591,47 @@ get_grab(PyObject *self, PyObject *_null)
15911591
returnPyBool_FromLong(mode);
15921592
}
15931593

1594+
staticPyObject*
1595+
set_keyboard_grab(PyObject*self,PyObject*arg)
1596+
{
1597+
#ifSDL_VERSION_ATLEAST(2,0,16)
1598+
VIDEO_INIT_CHECK();
1599+
1600+
intdoit=PyObject_IsTrue(arg);
1601+
if (doit==-1)
1602+
returnNULL;
1603+
1604+
SDL_Window*win=pg_GetDefaultWindow();
1605+
if (win) {
1606+
if (doit) {
1607+
SDL_SetWindowKeyboardGrab(win,SDL_TRUE);
1608+
}
1609+
else {
1610+
SDL_SetWindowKeyboardGrab(win,SDL_FALSE);
1611+
}
1612+
}
1613+
1614+
#endif
1615+
Py_RETURN_NONE;
1616+
}
1617+
1618+
staticPyObject*
1619+
get_keyboard_grab(PyObject*self,PyObject*_null)
1620+
{
1621+
#ifSDL_VERSION_ATLEAST(2,0,16)
1622+
SDL_Window*win;
1623+
SDL_boolmode=SDL_FALSE;
1624+
1625+
VIDEO_INIT_CHECK();
1626+
win=pg_GetDefaultWindow();
1627+
if (win)
1628+
mode=SDL_GetWindowKeyboardGrab(win);
1629+
returnPyBool_FromLong(mode);
1630+
#else
1631+
Py_RETURN_NONE;
1632+
#endif
1633+
}
1634+
15941635
staticvoid
15951636
_pg_event_pump(intdopump)
15961637
{
@@ -2216,6 +2257,10 @@ static PyMethodDef _event_methods[] = {
22162257

22172258
{"set_grab",set_grab,METH_O,DOC_PYGAMEEVENTSETGRAB},
22182259
{"get_grab", (PyCFunction)get_grab,METH_NOARGS,DOC_PYGAMEEVENTGETGRAB},
2260+
{"set_keyboard_grab",set_keyboard_grab,METH_O,
2261+
DOC_PYGAMEEVENTSETKEYBOARDGRAB},
2262+
{"get_keyboard_grab", (PyCFunction)get_keyboard_grab,METH_NOARGS,
2263+
DOC_PYGAMEEVENTGETKEYBOARDGRAB},
22192264

22202265
{"pump", (PyCFunction)pg_event_pump,METH_NOARGS,DOC_PYGAMEEVENTPUMP},
22212266
{"wait", (PyCFunction)pg_event_wait,METH_VARARGS |METH_KEYWORDS,

‎test/event_test.py‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
importcollections
22
importtime
33
importunittest
4+
importos
45

56
importpygame
67

@@ -839,6 +840,22 @@ def test_get_grab(self):
839840
pygame.event.set_grab(i%2)
840841
self.assertEqual(pygame.event.get_grab(),i%2)
841842

843+
@unittest.skipIf(
844+
os.environ.get("SDL_VIDEODRIVER")=="dummy",
845+
"requires the SDL_VIDEODRIVER to be a non dummy value",
846+
)
847+
@unittest.skipIf(pygame.get_sdl_version()< (2,0,16),"Needs at least SDL 2.0.16")
848+
deftest_set_keyboard_grab_and_get_keyboard_grab(self):
849+
"""Ensure set_keyboard_grab() and get_keyboard_grab() work as expected"""
850+
851+
surf=pygame.display.set_mode((10,10))
852+
853+
pygame.event.set_keyboard_grab(True)
854+
self.assertTrue(pygame.event.get_keyboard_grab())
855+
856+
pygame.event.set_keyboard_grab(False)
857+
self.assertFalse(pygame.event.get_keyboard_grab())
858+
842859
deftest_poll(self):
843860
"""Ensure poll() works as expected"""
844861
pygame.event.clear()

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp