-
Notifications
You must be signed in to change notification settings - Fork 200
/
Copy pathmodstate-monitor.lua
72 lines (60 loc) · 1.54 KB
/
modstate-monitor.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
-- Displays changes in the key modifier state
--@ enable = true
--[====[
devel/modstate-monitor
======================
Display changes in key modifier state, ie Ctrl/Alt/Shift.
:enable|start: Begin monitoring
:disable|stop: End monitoring
]====]
VERSION = '0.1'
if active == nil then active = false end
if dfhack.internal.getModstate == nil or dfhack.internal.getModifiers == nil then
qerror('Required internal functions are missing')
end
function usage()
print [[
Usage:
modstate-monitor enable|start: Begin monitoring
modstate-monitor disable|stop: End monitoring
]]
end
function set_timeout()
dfhack.timeout(1, 'frames', check)
end
function log(s, color)
-- prevent duplicate output
if s ~= last_msg then
dfhack.color(color)
print(s)
last_msg = s
end
end
function check()
local msg = ''
local modstate = dfhack.internal.getModstate()
if modstate ~= last_modstate then
last_modstate = modstate
for k, v in pairs(dfhack.internal.getModifiers()) do
msg = msg .. k .. '=' .. (v and 1 or 0) .. ' '
end
log(msg)
end
if active then set_timeout() end
end
args = {...}
if dfhack_flags and dfhack_flags.enable then
table.insert(args, dfhack_flags.enable_state and 'enable' or 'disable')
end
if #args == 1 then
if args[1] == 'enable' or args[1] == 'start' then
set_timeout()
active = true
elseif args[1] == 'disable' or args[1] == 'stop' then
active = false
else
usage()
end
else
usage()
end