Exemplo n.º 1
0
func (app *DemoApp) RunMessageLoop() {
	msg := w32.MSG{}
	for w32.GetMessage(&msg, 0, 0, 0) > 0 {
		w32.TranslateMessage(&msg)
		w32.DispatchMessage(&msg)
	}
}
Exemplo n.º 2
0
// Creates a low-level keyboard hook using the SetWindowsHookEx function:
// http://msdn.microsoft.com/en-us/library/windows/desktop/ms644990(v=vs.85).aspx
//
// Each intercepted key, which was included in the 'forwardedKeys' configuration
// variable (see NewKeyboardCapture), will be pushed to the 'KeyPressed' channel field.
// Returns an error in case the initialization of the hook failed.
// Calls to this function will block until KeyboardCapture.Stop() was called or the
// WM_QUIT message was sent to the current process.
func (t *KeyboardCapture) SyncReceive() error {
	isValidKey := func(key w32.DWORD) bool {
		for _, e := range t.forwardedKeys {
			if e == int(key) {
				return true
			}
		}
		return false
	}
	t.KeyPressed = make(chan int)
	t.keyboardHook = w32.SetWindowsHookEx(w32.WH_KEYBOARD_LL,
		(w32.HOOKPROC)(func(code int, wparam w32.WPARAM, lparam w32.LPARAM) w32.LRESULT {
			if code >= 0 && wparam == w32.WM_KEYDOWN {
				kbdstruct := (*w32.KBDLLHOOKSTRUCT)(unsafe.Pointer(lparam))
				if isValidKey(kbdstruct.VkCode) {
					select {
					case t.KeyPressed <- int(kbdstruct.VkCode):
					default:
					}
				}
			}
			return w32.CallNextHookEx(t.keyboardHook, code, wparam, lparam)
		}), 0, 0)
	if t.keyboardHook == 0 {
		return syscall.GetLastError()
	}
	var msg w32.MSG
	for w32.GetMessage(&msg, 0, 0, 0) != 0 {
	}
	w32.UnhookWindowsHookEx(t.keyboardHook)
	t.keyboardHook = 0

	return nil
}
Exemplo n.º 3
0
func (this *Window) HandleWndMessages() {
	var m w32.MSG

	for w32.GetMessage(&m, this.hwnd, 0, 0) != 0 {
		w32.TranslateMessage(&m)
		w32.DispatchMessage(&m)
	}
}
Exemplo n.º 4
0
func RunMainLoop() int {
	var m w32.MSG

	for w32.GetMessage(&m, 0, 0, 0) != 0 {
		if !PreTranslateMessage(&m) {
			w32.TranslateMessage(&m)
			w32.DispatchMessage(&m)
		}
	}

	w32.GdiplusShutdown()

	return int(m.WParam)
}
Exemplo n.º 5
0
func run() {
	msg := &w32.MSG{}
	for {
		switch val := w32.GetMessage(msg, 0, 0, 0); val {
		case -1:
			log.Printf("w32: error: %v\n", getLastError())
			os.Exit(1)
		case 0:
			return
		default:
			w32.TranslateMessage(msg)
			w32.DispatchMessage(msg)
		}
	}
}
Exemplo n.º 6
0
// Adds the notification icon and starts handling the mouse
// interaction with the icon.
// Calls to this function will block until the Stop() function
// will be called from another goroutine/thread.
// Returns an error, if adding the notify icons fails.
func (t *_NotifyIcon) Start() (err error) {
	log.Println("Creating notification icon")

	tooltipUtf16, _ := syscall.UTF16FromString(t.tooltip)
	t.nid.CbSize = w32.DWORD(unsafe.Sizeof(&t.nid))
	t.nid.HWnd = t.hwnd
	t.nid.UFlags = _NIF_MESSAGE | _NIF_ICON | _NIF_TIP
	t.nid.UID = niUID
	t.nid.UCallbackMessage = niCallbackMessage
	copy(t.nid.SzTip[:], tooltipUtf16)
	err = shellNotifyIcon(_NIM_ADD, &t.nid)
	if err != nil {
		return
	}

	var msg w32.MSG
	for w32.GetMessage(&msg, t.hwnd, uint32(0), uint32(0)) != 0 {
		w32.TranslateMessage(&msg)
		w32.DispatchMessage(&msg)
	}
	return nil
}