參考:https://gitee.com/veni0/robotgo#examples 可是編譯不成功git
找到這個:https://gitee.com/veni0/robotgo#examplesgithub
(這裏還有一篇參考:https://gitee.com/veni0/robotgo#examples )golang
原來仍是安裝的Mingw有問題apache
用了github上robotgo推薦的帶有zlib和libpng庫的gcc環境:ide
https://github.com/go-vgo/Mingw 工具
下載好以後無需安裝直接放到D盤,配置一下環境變量: D:\Mingw\bin 加入 PATH. 測試
gcc環境弄好以後。用如下代碼測試:this
package main import ( "github.com/go-vgo/robotgo" ) func main() { robotgo.ScrollMouse(10, "up") robotgo.MouseClick("left", true) robotgo.MoveMouseSmooth(100, 200, 1.0, 100.0) }
結果提示一堆錯誤,相似spa
d:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/4.8.2/../../../../x86_64-w64-mingw32/bin/ld.exe: skipping incompatible d:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/4.8.2/../../../../x86_64-w64-mingw32/lib/libmingwthrd.a when searching for -lmingwthrd d:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/4.8.2/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot find -lmingwthrd
一翻搜索,在https://stackoverflow.com/questions/14990222/skipping-incompatible-cannot-find的一位名叫Michael Burr的answer提醒了我,意思大概就是說須要32位的lib,因此不匹配當前的64位的libcode
因而修改liteide工具條上的環境設置,原來的是win32-user 改成 system 。liteide提示GOARCH 變成了 amd64. 從新Ctrl-R,成功運行。
這時發現鼠標已經不受控了,心中一驚。好在還有鍵盤快捷方式,用Alt+B 及光標中止了liteide的運行(其實當鼠標移動到位後程序就自動結束了)。
另外還有一個小插曲,下載解壓mingw後提示一堆的病毒,網上也有抱怨360的,由於360是公司要求裝的,不能卸載。最後將mingw目錄加入360的設置白名單。
補充一個例子,由於公司業務只能用IE,有個ActiveX。試試操做IE打開百度進行搜索,代碼以下:
package main import ( "os/exec" "github.com/go-vgo/robotgo" ) func main() { //啓動IE,打開百度 exec.Command("cmd", "/c", "start", "iexplore.exe", "www.baidu.com").Run() //等待 robotgo.MilliSleep(5 * 1000) // get current Window Active mdata := robotgo.GetActive() // set Window Active robotgo.SetActive(mdata) robotgo.TypeString("pu369") robotgo.KeyTap("space") robotgo.TypeString("golang") robotgo.KeyTap("enter") robotgo.MilliSleep(5 * 1000) // close current Window robotgo.CloseWindow() }
最後看了一下,github源代碼有個什麼Apache License,雖然咱屬於「合理利用」,也不知道應該和誰辯論合理不合理,仍是似懂非懂地按要求附上Apache License以下:
// 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.