Skip to content

Commit

Permalink
Merge pull request #550 from go-vgo/bitmap-pr
Browse files Browse the repository at this point in the history
Update: Optimize window hand code
  • Loading branch information
vcaesar authored Nov 28, 2022
2 parents 2f936e1 + 6313e52 commit 3052272
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 42 deletions.
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,9 @@ xcode-select --install
Download the Mingw, then set system environment variables `C:\mingw64\bin` to the Path.
[Set environment variables to run GCC from command line](https://www.youtube.com/results?search_query=Set+environment+variables+to+run+GCC+from+command+line).


```
`
Or the other GCC (But you should compile the "libpng" with yourself when use the bitmap.)
```
`

#### For everything else:

Expand Down
2 changes: 1 addition & 1 deletion doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ Keys are supported:
"f23"
"f24"
"cmd" is the "win" key for windows
"cmd" this is the "win" key for windows
"lcmd" left command
"rcmd" right command
// "command"
Expand Down
7 changes: 7 additions & 0 deletions key.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"github.com/vcaesar/tt"
)

// Defining a bunch of constants.
const (
// KeyA define key "a"
KeyA = "a"
Expand Down Expand Up @@ -321,6 +322,7 @@ var keyNames = map[string]C.MMKeyCode{
// { NULL: C.K_NOT_A_KEY }
}

// It sends a key press and release to the active application
func tapKeyCode(code C.MMKeyCode, flags C.MMKeyFlags, pid C.uintptr) {
C.toggleKeyCode(code, true, flags, pid)
MilliSleep(3)
Expand Down Expand Up @@ -459,6 +461,7 @@ func ToStrings(fields []interface{}) []string {
return res
}

// toErr it converts a C string to a Go error
func toErr(str *C.char) error {
gstr := C.GoString(str)
if gstr == "" {
Expand All @@ -481,6 +484,8 @@ func toErr(str *C.char) error {
//
// arr := []string{"alt", "command"}
// robotgo.KeyTap("i", arr)
//
// robotgo.KeyTap("k", pid int)
func KeyTap(key string, args ...interface{}) error {
var keyArr []string

Expand Down Expand Up @@ -525,6 +530,7 @@ func KeyTap(key string, args ...interface{}) error {
// robotgo.KeyToggle("a", "up")
//
// robotgo.KeyToggle("a", "up", "alt", "cmd")
// robotgo.KeyToggle("k", pid int)
func KeyToggle(key string, args ...interface{}) error {

if len(key) > 0 && unicode.IsUpper([]rune(key)[0]) {
Expand Down Expand Up @@ -648,6 +654,7 @@ func inputUTF(str string) {
// Examples:
//
// robotgo.TypeStr("abc@123, Hi galaxy, こんにちは")
// robotgo.TypeStr("To be or not to be, this is questions.", pid int)
func TypeStr(str string, args ...int) {
var tm, tm1 = 0, 7

Expand Down
31 changes: 22 additions & 9 deletions key/keypress_c.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
#include "keypress.h"
// Copyright 2016 The go-vgo Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// https://github.com/go-vgo/robotgo/blob/master/LICENSE
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#include "../base/deadbeef_rand_c.h"
#include "../base/microsleep.h"
#include "keypress.h"
#include "keycode_c.h"

#include <ctype.h> /* For isupper() */
Expand All @@ -17,6 +27,14 @@
#if defined(IS_WINDOWS)
HWND GetHwndByPid(DWORD dwProcessId);

HWND getHwnd(uintptr pid, int8_t isPid) {
HWND hwnd = (HWND) pid;
if (isPid == 0) {
hwnd = GetHwndByPid(pid);
}
return hwnd;
}

void WIN32_KEY_EVENT_WAIT(MMKeyCode key, DWORD flags, uintptr pid) {
win32KeyEvent(key, flags, pid, 0);
Sleep(DEADBEEF_RANDRANGE(0, 1));
Expand Down Expand Up @@ -115,10 +133,8 @@ void win32KeyEvent(int key, MMKeyFlags flags, uintptr pid, int8_t isPid) {

// todo: test this
if (pid != 0) {
HWND hwnd = (HWND) pid;
if (isPid == 0) {
hwnd = GetHwndByPid(pid);
}
HWND hwnd = getHwnd(pid, isPid);

int down = (flags == 0 ? WM_KEYDOWN : WM_KEYUP);
// SendMessage(hwnd, down, key, 0);
PostMessageW(hwnd, down, key, 0);
Expand Down Expand Up @@ -316,10 +332,7 @@ void unicodeType(const unsigned value, uintptr pid, int8_t isPid){
toggleUnicode(ch, false, pid);
#elif defined(IS_WINDOWS)
if (pid != 0) {
HWND hwnd = (HWND) pid;
if (isPid == 0) {
hwnd = GetHwndByPid(pid);
}
HWND hwnd = getHwnd(pid, isPid);

// SendMessage(hwnd, down, value, 0);
PostMessageW(hwnd, WM_CHAR, value, 0);
Expand Down
1 change: 1 addition & 0 deletions window/alert_c.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ int showAlert(const char *title, const char *msg,
CFStringRef defaultButtonTitle = CFStringCreateWithUTF8String(defaultButton);
CFStringRef cancelButtonTitle = CFStringCreateWithUTF8String(cancelButton);
CFOptionFlags responseFlags;

SInt32 err = CFUserNotificationDisplayAlert(
0.0, kCFUserNotificationNoteAlertLevel, NULL, NULL, NULL, alertHeader, alertMessage,
defaultButtonTitle, cancelButtonTitle, NULL, &responseFlags);
Expand Down
16 changes: 4 additions & 12 deletions window/goWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,8 @@ void min_window(uintptr pid, bool state, int8_t isPid){
XDismissErrors();
// SetState((Window)pid, STATE_MINIMIZE, state);
#elif defined(IS_WINDOWS)
if (isPid == 0) {
HWND hwnd = GetHwndByPid(pid);
win_min(hwnd, state);
} else {
win_min((HWND)pid, state);
}
HWND hwnd = getHwnd(pid, isPid);
win_min(hwnd, state);
#endif
}

Expand All @@ -40,12 +36,8 @@ void max_window(uintptr pid, bool state, int8_t isPid){
// SetState((Window)pid, STATE_MINIMIZE, false);
// SetState((Window)pid, STATE_MAXIMIZE, state);
#elif defined(IS_WINDOWS)
if (isPid == 0) {
HWND hwnd = GetHwndByPid(pid);
win_max(hwnd, state);
} else {
win_max((HWND)pid, state);
}
HWND hwnd = getHwnd(pid, isPid);
win_max(hwnd, state);
#endif
}

Expand Down
2 changes: 2 additions & 0 deletions window/pub.h
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,8 @@ typedef struct _Bounds Bounds;


#elif defined(IS_WINDOWS)
HWND getHwnd(uintptr pid, int8_t isPid);

void win_min(HWND hwnd, bool state){
if (state) {
ShowWindow(hwnd, SW_MINIMIZE);
Expand Down
14 changes: 2 additions & 12 deletions window/win_sys.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,12 +128,7 @@ Bounds get_bounds(uintptr pid, int8_t isPid){

return bounds;
#elif defined(IS_WINDOWS)
HWND hwnd;
if (isPid == 0) {
hwnd= GetHwndByPid(pid);
} else {
hwnd = (HWND)pid;
}
HWND hwnd = getHwnd(pid, isPid);

RECT rect = { 0 };
GetWindowRect(hwnd, &rect);
Expand Down Expand Up @@ -193,12 +188,7 @@ Bounds get_client(uintptr pid, int8_t isPid) {

return bounds;
#elif defined(IS_WINDOWS)
HWND hwnd;
if (isPid == 0) {
hwnd = GetHwndByPid(pid);
} else {
hwnd = (HWND)pid;
}
HWND hwnd = getHwnd(pid, isPid);

RECT rect = { 0 };
GetClientRect(hwnd, &rect);
Expand Down
6 changes: 1 addition & 5 deletions window/window.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,7 @@ MData set_handle_pid(uintptr pid, int8_t isPid){
win.XWin = (Window)pid; // Handle to an X11 window
#elif defined(IS_WINDOWS)
// win.HWnd = (HWND)pid; // Handle to a window HWND
if (isPid == 0) {
win.HWnd = GetHwndByPid(pid);
} else {
win.HWnd = (HWND)pid;
}
win.HWnd = getHwnd(pid, isPid);
#endif

return win;
Expand Down

0 comments on commit 3052272

Please sign in to comment.