Exemplo n.º 1
0
// GrabKeyboard grabs the entire keyboard.
// Returns whether GrabStatus is successful and an error if one is reported by
// XGB. It is possible to not get an error and the grab to be unsuccessful.
// The purpose of 'win' is that after a grab is successful, ALL Key*Events will
// be sent to that window. Make sure you have a callback attached :-)
func GrabKeyboard(xu *xgbutil.XUtil, win xproto.Window) error {
	reply, err := xproto.GrabKeyboard(xu.Conn(), false, win, 0,
		xproto.GrabModeAsync, xproto.GrabModeAsync).Reply()
	if err != nil {
		return fmt.Errorf("GrabKeyboard: Error grabbing keyboard on "+
			"window '%x': %s", win, err)
	}

	switch reply.Status {
	case xproto.GrabStatusSuccess:
		// all is well
	case xproto.GrabStatusAlreadyGrabbed:
		return fmt.Errorf("GrabKeyboard: Could not grab keyboard. " +
			"Status: AlreadyGrabbed.")
	case xproto.GrabStatusInvalidTime:
		return fmt.Errorf("GrabKeyboard: Could not grab keyboard. " +
			"Status: InvalidTime.")
	case xproto.GrabStatusNotViewable:
		return fmt.Errorf("GrabKeyboard: Could not grab keyboard. " +
			"Status: NotViewable.")
	case xproto.GrabStatusFrozen:
		return fmt.Errorf("GrabKeyboard: Could not grab keyboard. " +
			"Status: Frozen.")
	}
	return nil
}
Exemplo n.º 2
0
// Lock grabs the keyboard and pointer locking the X11 display
func Lock(X *xgb.Conn) error {
	screen := xproto.Setup(X).DefaultScreen(X)

	passEventsToOwner := false
	kbCookie := xproto.GrabKeyboard(X, passEventsToOwner, screen.Root, xproto.TimeCurrentTime, xproto.GrabModeAsync, xproto.GrabModeAsync)
	kbReply, err := kbCookie.Reply()
	if err != nil {
		return err
	}
	if kbReply.Status != xproto.GrabStatusSuccess {
		return fmt.Errorf("GrabKeyboard status %v", grabStatus(kbReply.Status))
	}

	ptrCookie := xproto.GrabPointer(X, passEventsToOwner, screen.Root, 0, xproto.GrabModeAsync, xproto.GrabModeAsync, xproto.AtomNone, xproto.AtomNone, xproto.TimeCurrentTime)

	ptrReply, err := ptrCookie.Reply()
	if err != nil {
		xproto.UngrabKeyboard(X, xproto.TimeCurrentTime)
		return err
	}
	if ptrReply.Status != xproto.GrabStatusSuccess {
		return fmt.Errorf("GrabPointer status %v", grabStatus(kbReply.Status))
	}

	return nil
}