Keymon.refresh

From OpenEUO
Revision as of 16:36, 8 January 2011 by Ximan (Talk | contribs) (Created page with "== Calling Pattern == Call local km = sl.keymon(t) later keymon.refresh() == Example == local t = { ['ALT SHIFT J']= function() print('ALT SHIFT J') end, ['CTRL J'] ...")

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Calling Pattern

Call

local km = sl.keymon(t)

later

keymon.refresh()

Example

local t = {
  ['ALT SHIFT J']= function() print('ALT SHIFT J') end,
  ['CTRL J']     = function() print('CTRL J')      end,
}
local km = sl.keymon(t)
while true do
  km.refresh()
  wait(30)
end
-> CTRL J
   ALT SHIFT J
   ALT SHIFT J

Description

The keymon refresh method the state of every key necessary and determines which, if any, hotkey is currently pressed. The keymon generator creates a string from the given table of hotkey=handlerfunction values to quickly assess if any handler should be called and passes this to loadstring. In the example above, the created internal function looks like:

local gk = {getkey("ALT"),getkey("SHIFT"),getkey("CTRL"),getkey("J"),};
if  gk[1] == true and gk[2] == true and gk[3] == false and gk[4] == true then return 1 
elseif  gk[1] == false and gk[2] == false and gk[3] == true and gk[4] == true then return 2 
end;

The result of calling this function is used to look up and invoke the appropriate user supplied handler. Refresh takes no arguments and normally returns no values.

Upon Error

If one of the hotkey handler functions throws an error during evaluation, an unhandled exception will occur. In versions 0.09+, any errors thrown will be caught at the keymon.refresh level and handled according to the operant error redirection mode.

See Also