├── README.md ├── go.mod ├── lib └── libwindevice.a ├── read.txt ├── cdep ├── windevice.c └── windevice.h ├── windev.go ├── exam └── main │ └── main.go └── macro.go /README.md: -------------------------------------------------------------------------------- 1 | # windev 2 | win下监听鼠标和键盘事件 3 | -------------------------------------------------------------------------------- /go.mod: -------------------------------------------------------------------------------- 1 | module github.com/cppdebug/windev 2 | 3 | go 1.13 4 | -------------------------------------------------------------------------------- /lib/libwindevice.a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cppdebug/windev/HEAD/lib/libwindevice.a -------------------------------------------------------------------------------- /read.txt: -------------------------------------------------------------------------------- 1 | 0, 此库只能用在win下, 并且代码以及宏定义全部来自win c/c++ 2 | 1, 为了防止有些用户没有win c/c++的环境, 我已经编译好了一个库放在./lib下, 可以直接调用 3 | , 但是还是需要gcc的编译器, 因为是依赖cgo 4 | 2, 如果自己想编译的话, 确保c/c++环境, 然后删除./lib/libwindevice.a, 进入到./cdep目录 5 | 执行以下命令后, 移动到../lib目录下: 6 | a) gcc -Wall -c windevice.c 7 | b) ar -rv libwindevice.a windevice.o 8 | 3, 例子可以看下./exam/main/main.go 9 | 10 | -------------------------------------------------------------------------------- /cdep/windevice.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "windevice.h" 3 | 4 | int keyDownUp(int e) 5 | { 6 | return (GetAsyncKeyState(e) & 0x8000) ? 1:0; 7 | } 8 | 9 | bool setCursorPos(int x, int y) 10 | { 11 | return SetCursorPos(x, y); 12 | } 13 | 14 | void mouseEvent(int dwFlags, int dx, int dy, int dwData, long long dwExtraInfo) 15 | { 16 | mouse_event(dwFlags, dx, dy, dwData, dwExtraInfo); 17 | } -------------------------------------------------------------------------------- /cdep/windevice.h: -------------------------------------------------------------------------------- 1 | #ifndef WINDEVICE_H_ 2 | #define WINDEVICE_H_ 3 | 4 | #include 5 | 6 | #ifdef __cplusplus 7 | extern "C" { 8 | #endif 9 | 10 | // 键按下或者抬起 11 | // 1-处于按下状态 12 | // 0-处于抬起状态 13 | int keyDownUp(int e); 14 | // 设置屏幕坐标 15 | bool setCursorPos(int x, int y); 16 | // 鼠标事件 17 | void mouseEvent(int dwFlags, int dx, int dy, int dwData, long long dwExtraInfo); 18 | 19 | #ifdef __cplusplus 20 | } 21 | #endif 22 | 23 | #endif // WINDEVICE_H_ 24 | -------------------------------------------------------------------------------- /windev.go: -------------------------------------------------------------------------------- 1 | package windev 2 | /* 3 | #cgo CFLAGS: -I./cdep 4 | #cgo LDFLAGS: -L./lib -lwindevice 5 | #include "windevice.h" 6 | */ 7 | import "C" 8 | 9 | // KeyDownUp .. 10 | func KeyDownUp(e int) C.int{ 11 | return C.keyDownUp(C.int(e)) 12 | } 13 | 14 | // SetCursorPos .. 15 | func SetCursorPos(x, y int) C.bool{ 16 | return C.setCursorPos(C.int(x), C.int(y)) 17 | } 18 | 19 | // MouseEvent .. 20 | func MouseEvent(dwFlags, dx, dy, dwData int, dwExtraInfo int64) { 21 | C.mouseEvent(C.int(dwFlags), C.int(dx), C.int(dy), C.int(dwData), C.longlong(dwExtraInfo)) 22 | } 23 | -------------------------------------------------------------------------------- /exam/main/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "os" 6 | "time" 7 | 8 | "github.com/cppdebug/windev" 9 | ) 10 | 11 | func main() { 12 | 13 | for i := 0; i < 10; i++ { 14 | 15 | // 按Q键退出 16 | if windev.KeyDownUp(windev.VK_CHARQ) == 1 { 17 | fmt.Println("exit") 18 | os.Exit(-1) 19 | } 20 | 21 | // 设置鼠标的位置 22 | if windev.SetCursorPos(910, 300) { 23 | fmt.Println("set pos") 24 | } 25 | 26 | // 鼠标的滑轮事件, 页面向下滑动 27 | windev.MouseEvent(windev.MOUSEEVENTF_WHEEL, 0, 0, -100, 0) 28 | 29 | // 鼠标左键按下和松开事件 30 | windev.MouseEvent(windev.MOUSEEVENTF_LEFTDOWN|windev.MOUSEEVENTF_LEFTUP, 0, 0, 0, 0) 31 | 32 | time.Sleep(time.Second) 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /macro.go: -------------------------------------------------------------------------------- 1 | package windev 2 | 3 | /* 4 | 这些宏定义来自winuser.h 5 | 只摘了一部分过来, 有兴趣的朋友可以查看头文件 6 | */ 7 | 8 | // 鼠标事件集合 9 | const MOUSEEVENTF_MOVE = 0x0001 /* mouse move */ 10 | const MOUSEEVENTF_LEFTDOWN = 0x0002 /* left button down */ 11 | const MOUSEEVENTF_LEFTUP = 0x0004 /* left button up */ 12 | const MOUSEEVENTF_RIGHTDOWN = 0x0008 /* right button down */ 13 | const MOUSEEVENTF_RIGHTUP = 0x0010 /* right button up */ 14 | const MOUSEEVENTF_MIDDLEDOWN = 0x0020 /* middle button down */ 15 | const MOUSEEVENTF_MIDDLEUP = 0x0040 /* middle button up */ 16 | const MOUSEEVENTF_XDOWN = 0x0080 /* x button down */ 17 | const MOUSEEVENTF_XUP = 0x0100 /* x button down */ 18 | const MOUSEEVENTF_WHEEL = 0x0800 /* wheel button rolled */ 19 | 20 | // 键盘事件集合 21 | const VK_LBUTTON = 0x01 // 鼠标左键 22 | const VK_RBUTTON = 0x02 // 鼠标右键 23 | const VK_MBUTTON = 0x04 // 鼠标中键 24 | const VK_BACK = 0x08 // BackSpace 25 | const VK_TAB = 0x09 // Tab 26 | const VK_CLEAR = 0x0C // Num Lock关闭时的数字区 5 27 | const VK_RETURN = 0x0D // Enter 28 | const VK_SHIFT = 0x10 // Shift/左右shift都可以 29 | const VK_CONTROL = 0x11 // Ctrl/左右Ctrl都可以 30 | const VK_MENU = 0x12 // Alt/左右Alt都可以 31 | const VK_PAUSE = 0x13 /* Pause*/ 32 | const VK_CAPITAL = 0x14 // Caps Lock 33 | const VK_ESCAPE = 0x1B // Esc 34 | const VK_SPACE = 0x20 // Space 35 | const VK_PRIOR = 0x21 // PgUp 36 | const VK_NEXT = 0x22 // PgDn 37 | const VK_END = 0x23 // End 38 | const VK_HOME = 0x24 // Home 39 | const VK_LEFT = 0x25 // 方向左 40 | const VK_UP = 0x26 // 方向上 41 | const VK_RIGHT = 0x27 // 方向右 42 | const VK_DOWN = 0x28 // 方向下 43 | const VK_SELECT = 0x29 /* Select*/ 44 | const VK_PRINT = 0x2A /* Print*/ 45 | const VK_EXECUTE = 0x2B /* Execute*/ 46 | const VK_SNAPSHOT = 0x2C /* Print Screen键(抓屏)*/ 47 | const VK_INSERT = 0x2D // Insert 48 | const VK_DELETE = 0x2E // Delete 49 | const VK_HELP = 0x2F /* Help*/ 50 | const VK_CHAR0 = 0x30 // '0' 51 | const VK_CHAR1 = 0x31 // '1' 52 | const VK_CHAR2 = 0x32 // '2' 53 | const VK_CHAR3 = 0x33 // '3' 54 | const VK_CHAR4 = 0x34 // '4' 55 | const VK_CHAR5 = 0x35 // '5' 56 | const VK_CHAR6 = 0x36 // '6' 57 | const VK_CHAR7 = 0x37 // '7' 58 | const VK_CHAR8 = 0x38 // '8' 59 | const VK_CHAR9 = 0x39 // '9' 60 | const VK_CHARA = 0x41 // 'A' 61 | const VK_CHARB = 0x42 // 'B' 62 | const VK_CHARC = 0x43 // 'C' 63 | const VK_CHARD = 0x44 // 'D' 64 | const VK_CHARE = 0x45 // 'E' 65 | const VK_CHARF = 0x46 // 'F' 66 | const VK_CHARG = 0x47 // 'G' 67 | const VK_CHARH = 0x48 // 'H' 68 | const VK_CHARI = 0x49 // 'I' 69 | const VK_CHARJ = 0x4A // 'J' 70 | const VK_CHARK = 0x4B // 'K' 71 | const VK_CHARL = 0x4C // 'L' 72 | const VK_CHARM = 0x4D // 'M' 73 | const VK_CHARN = 0x4E // 'N' 74 | const VK_CHARO = 0x4F // 'O' 75 | const VK_CHARP = 0x50 // 'P' 76 | const VK_CHARQ = 0x51 // 'Q' 77 | const VK_CHARR = 0x52 // 'R' 78 | const VK_CHARS = 0x53 // 'S' 79 | const VK_CHART = 0x54 // 'T' 80 | const VK_CHARU = 0x55 // 'U' 81 | const VK_CHARV = 0x56 // 'V' 82 | const VK_CHARW = 0x57 // 'W' 83 | const VK_CHARX = 0x58 // 'X' 84 | const VK_CHARY = 0x59 // 'Y' 85 | const VK_CHARZ = 0x5A // 'Z' 86 | const VK_LWIN = 0x5B // 左win键 87 | const VK_RWIN = 0x5C // 右win键 88 | const VK_NUMPAD0 = 0x60 // 数字区 0 89 | const VK_NUMPAD1 = 0x61 // 数字区 1 90 | const VK_NUMPAD2 = 0x62 // 数字区 2 91 | const VK_NUMPAD3 = 0x63 // 数字区 3 92 | const VK_NUMPAD4 = 0x64 // 数字区 4 93 | const VK_NUMPAD5 = 0x65 // 数字区 5 94 | const VK_NUMPAD6 = 0x66 // 数字区 6 95 | const VK_NUMPAD7 = 0x67 // 数字区 7 96 | const VK_NUMPAD8 = 0x68 // 数字区 8 97 | const VK_NUMPAD9 = 0x69 // 数字区 9 98 | const VK_MULTIPLY = 0x6A // 数字区 * 99 | const VK_ADD = 0x6B // 数字区 + 100 | const VK_SEPARATOR = 0x6C /* Sepatator*/ 101 | const VK_SUBTRACT = 0x6D // 数字区 - 102 | const VK_DECIMAL = 0x6E // 数字区 小数点 103 | const VK_DIVIDE = 0x6F // 数字区 / 104 | const VK_F1 = 0x70 // F1 105 | const VK_F2 = 0x71 // F2 106 | const VK_F3 = 0x72 // F3 107 | const VK_F4 = 0x73 // F4 108 | const VK_F5 = 0x74 // F5 109 | const VK_F6 = 0x75 // F6 110 | const VK_F7 = 0x76 // F7 111 | const VK_F8 = 0x77 // F8 112 | const VK_F9 = 0x78 // F9 113 | const VK_F10 = 0x79 // F10 114 | const VK_F11 = 0x7A // F11 115 | const VK_F12 = 0x7B // F12 116 | const VK_F13 = 0x7C // F13 117 | const VK_F14 = 0x7D // F14 118 | const VK_F15 = 0x7E // F15 119 | const VK_F16 = 0x7F // F16 120 | const VK_F17 = 0x80 // F17 121 | const VK_F18 = 0x81 // F18 122 | const VK_F19 = 0x82 // F19 123 | const VK_F20 = 0x83 // F20 124 | const VK_F21 = 0x84 // F21 125 | const VK_F22 = 0x85 // F22 126 | const VK_F23 = 0x86 // F23 127 | const VK_F24 = 0x87 // F24 128 | const VK_NUMLOCK = 0x90 // Num Lock 129 | const VK_SCROLL = 0x91 /* Scroll Lock*/ 130 | const VK_LSHIFT = 0xA0 // 左shift 131 | const VK_RSHIFT = 0xA1 // 右shift 132 | const VK_LCONTROL = 0xA2 // 左Ctrl 133 | const VK_RCONTROL = 0xA3 // 右Ctrl 134 | const VK_LMENU = 0xA4 // 左Alt 135 | const VK_RMENU = 0xA5 // 右Alt 136 | const VK_OEM_1 = 0xBA // ';:' 137 | const VK_OEM_PLUS = 0xBB // '=+' 138 | const VK_OEM_COMMA = 0xBC // ',<' 139 | const VK_OEM_MINUS = 0xBD // '-_' 140 | const VK_OEM_PERIOD = 0xBE // '.>' 141 | const VK_OEM_2 = 0xBF // '/?' 142 | const VK_OEM_3 = 0xC0 // '`~' 143 | const VK_OEM_4 = 0xDB // '[{' 144 | const VK_OEM_5 = 0xDC // '\|' 145 | const VK_OEM_6 = 0xDD // ']}' 146 | const VK_OEM_7 = 0xDE // ''"' 147 | --------------------------------------------------------------------------------