Skip to content

Commit dc8fa0f

Browse files
committed
Add a grab/ungrab context manager
1 parent f8fc819 commit dc8fa0f

2 files changed

Lines changed: 18 additions & 0 deletions

File tree

‎docs/tutorial.rst‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,13 @@ Getting exclusive access to a device
267267
>>> dev.grab() # become the sole recipient of all incoming input events
268268
>>> dev.ungrab()
269269

270+
This functionality is also available as a context manager.
271+
272+
::
273+
274+
>>> with dev.grab_context():
275+
... pass
276+
270277

271278
Associating classes with event types
272279
====================================

‎evdev/device.py‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# encoding: utf-8
22

33
import os
4+
import contextlib
45
import collections
56

67
from evdev import _input, ecodes, util
@@ -307,6 +308,16 @@ def ungrab(self):
307308

308309
_input.ioctl_EVIOCGRAB(self.fd, 0)
309310

311+
@contextlib.contextmanager
312+
def grab_context(self):
313+
'''
314+
A context manager for the duration of which only the current
315+
process will be able to receive events from the device.
316+
'''
317+
self.grab()
318+
yield
319+
self.ungrab()
320+
310321
def upload_effect(self, effect):
311322
'''
312323
Upload a force feedback effect to a force feedback device.

0 commit comments

Comments
 (0)