progrium/darwinkit

How to monitor shortcut behavior

Closed this issue · 5 comments

environment

  • macos verison: 11.2.3
  • github.com/progrium/macdriver v0.0.3-0.20210330171321-295658df6845

describe

I want to develop a shortcut function. But I couldn't capture the user's keyboard press event.

	events := make(chan cocoa.NSEvent)
	go func() {
		for e := range events {
			fmt.Println(e)
		}
	}()

	cocoa.NSEvent_GlobalMonitorMatchingMask(cocoa.NSEventMaskAny, events)

image

Only mouse/ctrl/option/command press events can be captured.

expect

Is there any other good way? or any example? thx.

wait, where is the code that generates events for that channel?

I only try to modify the code from https://github.com/progrium/macdriver/blob/main/examples/_topframe/main.go

The original code was

		events := make(chan cocoa.NSEvent)
		go func() {
			for e := range events {
				if e.KeyCode() == 100 {
					if w.IgnoresMouseEvents() {
						fmt.Println("Mouse events on")
						w.SetIgnoresMouseEvents(false)
					} else {
						fmt.Println("Mouse events off")
						w.SetIgnoresMouseEvents(true)
					}
				}
				e.Release()
			}
		}()
		cocoa.NSEvent_GlobalMonitorMatchingMask(cocoa.NSEventMaskKeyDown, events)

Yeah actually I'm not sure I even got that working. Might have to hit up Apple docs and see what we need to add here.

tmc commented

I have a very basic start of a tool to show typed keys in an overlay: https://github.com/tmc/keyshow

This shows how to capture all keyboard events.

@tmc Awesome! That's what I want. Thank you very much.