refactor AddEvent() func and add keycode.go, update example

This commit is contained in:
vcaesar 2019-02-21 09:43:48 -04:00
parent b69982534b
commit 5ef1a4ee70
3 changed files with 100 additions and 34 deletions

View File

@ -21,19 +21,19 @@ func add() {
fmt.Println("--- Please press v---")
eve := robotgo.AddEvent("v")
if eve == 0 {
if eve {
fmt.Println("--- You press v---", "v")
}
fmt.Println("--- Please press k---")
keve := robotgo.AddEvent("k")
if keve == 0 {
if keve {
fmt.Println("--- You press k---", "k")
}
fmt.Println("--- Please press f1---")
feve := robotgo.AddEvent("f1")
if feve == 0 {
if feve {
fmt.Println("You press...", "f1")
}
}
@ -47,12 +47,12 @@ func event() {
fmt.Println("--- Please press left mouse button---")
mleft := robotgo.AddEvent("mleft")
if mleft == 0 {
if mleft {
fmt.Println("--- You press left mouse button---", "mleft")
}
mright := robotgo.AddEvent("mright")
if mright == 0 {
if mright {
fmt.Println("--- You press right mouse button---", "mright")
}

88
keycode.go Normal file
View File

@ -0,0 +1,88 @@
// 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.
package robotgo
var keycode = Map{
"`": 41,
"1": 2,
"2": 3,
"3": 4,
"4": 5,
"5": 6,
"6": 7,
"7": 8,
"8": 9,
"9": 10,
"0": 11,
"-": 12,
"+": 13,
//
"q": 16,
"w": 17,
"e": 18,
"r": 19,
"t": 20,
"y": 21,
"u": 22,
"i": 23,
"o": 24,
"p": 25,
"[": 26,
"]": 27,
"\\": 28,
//
"a": 30,
"s": 31,
"d": 32,
"f": 33,
"g": 34,
"h": 35,
"j": 36,
"k": 37,
"l": 38,
";": 39,
"'": 40,
//
"z": 44,
"x": 45,
"c": 46,
"v": 47,
"b": 48,
"n": 49,
"m": 50,
",": 51,
".": 52,
"/": 53,
//
"f1": 59,
"f2": 60,
"f3": 61,
"f4": 62,
"f5": 63,
"f6": 64,
"f7": 65,
"f8": 66,
"f9": 67,
"f10": 68,
"f11": 69,
"f12": 70,
// more
"esc": 1,
"tab": 15,
"ctrl": 29,
"control": 29,
"alt": 56,
"space": 57,
"shift": 42,
"enter": 28,
"cmd": 3675,
"command": 3675,
}

View File

@ -1198,33 +1198,7 @@ func GetImgSize(imgPath string) (int, int) {
//
// mouse arguments: mleft, mright, wheelDown, wheelUp,
// wheelLeft, wheelRight.
func AddEvent(key string) int {
keycode := Map{
"f1": "59",
"f2": "60",
"f3": "61",
"f4": "62",
"f5": "63",
"f6": "64",
"f7": "65",
"f8": "66",
"f9": "67",
"f10": "68",
"f11": "69",
"f12": "70",
// more
"esc": "11",
"tab": "15",
"ctrl": "29",
"control": "29",
"alt": "56",
"space": "57",
"shift": "42",
"enter": "28",
"cmd": "3675",
"command": "3675",
}
func AddEvent(key string) bool {
var (
// cs *C.char
mArr = []string{"mleft", "mright", "wheelDown",
@ -1239,13 +1213,17 @@ func AddEvent(key string) int {
}
if len(key) > 1 && !mouseBool {
key = keycode[key].(string)
key = strconv.Itoa(keycode[key].(int))
}
geve := hook.AddEvent(key)
// defer C.free(unsafe.Pointer(cs))
return geve
if geve == 0 {
return true
}
return false
}
// StopEvent stop event listener