HSEventTap
TypeAn event tap watcher that intercepts input events from the system.
Obtain instances via hs.eventtap.addWatcher() — do not instantiate directly.
Monitoring keyboard events
const tap = hs.eventtap.addWatcher([hs.eventtap.eventTypes.keyDown], (event) => {
console.log("Key pressed: " + event.keyCode)
})
Properties
listenOnly
boolean
Whether this tap was created as listen-only (events are observed but never modified or suppressed)
Methods
start() -> HSEventTap
Start receiving events. Requires Accessibility permission.
Declaration
start() -> HSEventTap
Returns
HSEventTap
This tap, for chaining
Example
const tap = hs.eventtap.addWatcher([hs.eventtap.eventTypes.keyDown], (event) => {
console.log("Key: " + event.keyCode)
})
tap.start()
stop() -> HSEventTap
Stop receiving events
Declaration
stop() -> HSEventTap
Returns
HSEventTap
This tap, for chaining
Example
tap.stop()
setCallback(callback) -> HSEventTap
Replace the callback function
Declaration
setCallback(callback) -> HSEventTap
Parameters
| Name | Type | Description |
|---|---|---|
| callback | function | A function called for each matching event. Return false to consume (suppress) the event; return anything else to pass it through. |
Returns
HSEventTap
This tap, for chaining
Example
tap.setCallback((event) => {
console.log("Key: " + event.keyCode)
return false // consume the event
})
isEnabled() -> boolean
Whether this tap is currently active
Declaration
isEnabled() -> boolean
Returns
boolean
True if the tap is running
Example
console.log("Running: " + tap.isEnabled())
isCreated() -> boolean
Whether this tap has been registered with macOS
Declaration
isCreated() -> boolean
Returns
boolean
True if the tap has been created
Example
console.log("Created: " + tap.isCreated())