forked from DFHack/scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodstate-monitor.lua
More file actions
76 lines (63 loc) · 1.65 KB
/
modstate-monitor.lua
File metadata and controls
76 lines (63 loc) · 1.65 KB
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
73
74
75
76
-- Displays changes in the key modifier state
--@ enable = true
--[====[
devel/modstate-monitor
======================
Display changes in key modifier state, i.e. :kbd:`Ctrl`/:kbd:`Alt`/:kbd:`Shift`.
Arguments:
:enable|start: Begin monitoring
:disable|stop: End monitoring
]====]
VERSION = '0.1'
active = active or false
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
last_msg = last_msg or nil --as:string
function log(s, color)
-- prevent duplicate output
if s ~= last_msg then
dfhack.color(color)
print(s)
last_msg = s
end
end
last_modstate = last_modstate or nil --as:number
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
local 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