前言
羅技鼠標(biāo)的驅(qū)動中可以通過編寫宏的方式實(shí)現(xiàn)任意字符串的輸出,可是我在WPS中使用時會出現(xiàn)bug炬太,整個字符串會變?yōu)榈谝粋€字符的重復(fù)灸蟆,不清楚這是我電腦的個例還是普遍情況。為了解決這個問題亲族,我就想到了WinCompose這個軟件炒考,當(dāng)初寫qmk鍵盤固件的時候第一次接觸到該軟件,按下組合鍵后輸入一段序列就可以直接輸出預(yù)先定義好的字符串霎迫。Lua腳本搭配上該軟件可以實(shí)現(xiàn)一些比較好玩的功能斋枢。
WinCompose
首先給出該項(xiàng)目地址:
https://github.com/samhocevar/wincompose
A compose key for Windows, free and open-source, created by Sam Hocevar.
A compose key allows to easily write special characters such as é ? à ō ? ? ? ¤ ? ? ? ? ? ? ? ? using short and often very intuitive key combinations. For instance, ? is obtained using o + ", and ? is obtained using < + 3.
WinCompose also supports Emoji input for ?? ?? ?? ?? ?? ?? ??.
功能實(shí)現(xiàn)
-- WinCompose軟件中設(shè)定的組合鍵
local WINC_KEY = "ralt"
-- 輸入序列時按鍵的間隔,過小會出現(xiàn)軟件反應(yīng)不及時的情況
local WINC_INTERVAL = 10
function TypeUnicodeWords(words)
local word, i
-- 正則匹配unicode編碼
for word in string.gmatch(words, "%\\u(%w+)") do
-- 輸入WinCompose序列
PressAndReleaseKey(WINC_KEY)
Sleep(WINC_INTERVAL)
PressAndReleaseKey("u")
for i=1, #word do
Sleep(WINC_INTERVAL)
PressAndReleaseKey(string.sub(word, i, i))
end
Sleep(WINC_INTERVAL)
PressAndReleaseKey("enter")
end
end
上面的代碼中只給出了關(guān)鍵參數(shù)和功能函數(shù)知给,具體的應(yīng)用可以看下面的應(yīng)用實(shí)例瓤帚。
因?yàn)榱_技的腳本中不支持中文,所有使用這個函數(shù)的時候需要提前對字符串進(jìn)行unicode編碼轉(zhuǎn)換涩赢,最簡單的方法就是直接找一個在線轉(zhuǎn)換工具戈次,比如下面這個網(wǎng)站:
https://tool.ip138.com/ascii/
在腳本中要注意使用[[ ]]
這種忽略轉(zhuǎn)義的字符串表示形式,例如:
local example = [[\u6d4b\u8bd5\u6587\u5b57\u000a\u0061\u0062\u0043\u0044\u000a\uff0c\u3002\u002c\u002e]]
應(yīng)用實(shí)例
人一天之中最常問自己的問題是什么呢筒扒?毫無疑問就是“吃什么”怯邪,本次的實(shí)例就是編寫一個可以回答這一問題的腳本。按下功能鍵后就會輸出預(yù)定義菜單中隨機(jī)一個選項(xiàng)花墩。
-- WinCompose軟件中設(shè)定的組合鍵
local WINC_KEY = "ralt"
-- 輸入序列時按鍵的間隔悬秉,過小會出現(xiàn)軟件反應(yīng)不及時的情況
local WINC_INTERVAL = 10
-- 菜單,weight可取任意自然數(shù)冰蘑,數(shù)值越大和泌,隨機(jī)到的可能性越大
local MENU = {
-- 餃子
{name=[[\u997a\u5b50]], weight=4},
-- 漢堡
{name=[[\u6c49\u5821]], weight=0},
-- 披薩
{name=[[\u62ab\u8428]], weight=2},
-- 螺螄粉
{name=[[\u87ba\u86f3\u7c89]], weight=1},
}
-- 是否檢測鼠標(biāo)左鍵的相關(guān)事件
EnablePrimaryMouseButtonEvents(false)
function OnEvent(event, arg)
if (event == "MOUSE_BUTTON_PRESSED" and arg == 7) then
RandomEat(MENU)
end
end
function RandomEat(menu)
-- 創(chuàng)建局部變量
local t = {}
local total = 0
local key, item, target
-- 根據(jù)menu創(chuàng)建權(quán)值表
for key, item in pairs(menu) do
total = total + item.weight
t[key] = total
end
-- 根據(jù)總權(quán)值roll點(diǎn)
math.randomseed(GetRunningTime())
target = math.random(t[#t])
-- 根據(jù)點(diǎn)數(shù)在權(quán)值表中查找對應(yīng)序列號
for key, item in pairs(t) do
if (target <= item) then
TypeUnicodeWords(menu[key].name)
return
end
end
end
function TypeUnicodeWords(words)
local word, i
-- 正則匹配unicode編碼
for word in string.gmatch(words, "%\\u(%w+)") do
-- 輸入WinCompose序列
PressAndReleaseKey(WINC_KEY)
Sleep(WINC_INTERVAL)
PressAndReleaseKey("u")
for i=1, #word do
Sleep(WINC_INTERVAL)
PressAndReleaseKey(string.sub(word, i, i))
end
Sleep(WINC_INTERVAL)
PressAndReleaseKey("enter")
end
end