boxmein

KSNS element for cracker64 manager

Jun 7th, 2013
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.77 KB | None | 0 0
  1. --VER 1.1 UPDATE http://pastebin.com/raw.php?i=8Zuy6iB7
  2. -- latest in https://gist.github.com/boxmein/5730768
  3. -- Keyboard sensor element
  4. -- As per request by thread: http://tpt.io/:17012
  5.  
  6. -- Set TMP to the key code(!) to enable
  7. -- set TMP2 (optional) to the modifier code to set modifiers
  8. -- Use DEBUG to see the values before setting them
  9.  
  10. -- by boxmein with tremendous help from cracker64 and jacob1
  11.  
  12.  
  13. local el = elements.allocate("boxmein", "ksns")
  14.  
  15. local lastkeyn = 0
  16. local lastmod = 0
  17. local pressing = false
  18. local DEBUG = true
  19. local typ = ""
  20.  
  21. function update (index, partx, party, surround_space, nt)
  22.   local tmp = tpt.get_property("tmp", index)
  23.   local tmp2 = tpt.get_property("tmp2", index)
  24.  
  25.  
  26.   if pressing then
  27.     -- allow tmp2 to set key modifications (for the sake of shift, ctrl, etc)
  28.     if tmp2 > 0 and lastmod ~= tmp2 then return 0 end
  29.  
  30.  
  31.     if tmp == lastkeyn then
  32.       -- sparkle sparkle bitches
  33.       local t = {sim.partNeighbours(partx, party, 2)}
  34.  
  35.       -- why the fork would t be nil with particles nearby ;_;
  36.       if t == nil then return 0 end
  37.  
  38.       for _, v in ipairs(t) do
  39.         if bit.band(elements.property(tpt.get_property("type", v),"Properties"),32) == 32 then
  40.          
  41.           local rx, ry = sim.partPosition(v)
  42.           tpt.create(rx, ry, 15) -- SPRK
  43.         end
  44.       end
  45.     end
  46.   end
  47. end
  48.  
  49.  
  50. function key (keys, keyn, mod, evt)
  51.   lastmod = mod
  52.   if evt == 1 then
  53.     -- pressed
  54.     lastkeyn = keyn
  55.     pressing = true
  56.   elseif evt == 2 then
  57.     -- released
  58.     lastkeyn = keyn
  59.     pressing = false
  60.   end
  61. end
  62.  
  63. elements.element(el, elements.element(elements.DEFAULT_PT_DMND))
  64. elements.property(el, "Name", "KSNS")
  65. elements.property(el, "Colour", "0xDEADBEEF")
  66. elements.property(el, "Description", "Keyboard Sensor. Set tmp to key code, (tmp2 to modifier code - optional) to use.")
  67. elements.property(el, "MenuSection", SC_SENSOR)
  68.  
  69.  
  70. tpt.register_keypress(key)
  71. tpt.element_func(update, el)
  72.  
  73. -- # ====================================================================== # --
  74. -- # Debugging
  75.  
  76. if DEBUG then
  77.   -- for the sake of debug only, some might find it of use
  78.   function getmodtext(lastmod)
  79.     modtext = ""
  80.     if lastmod - 256 > 0 then
  81.       modtext = modtext .. "ALT "
  82.       lastmod = lastmod-256
  83.     end
  84.     if lastmod - 64 > 0 then
  85.       modtext = modtext .. "CTRL "
  86.       lastmod = lastmod-64
  87.     end
  88.     if lastmod - 1 > 0 then
  89.       modtext = modtext .. "SHIFT "
  90.       lastmod = lastmod-1
  91.     end
  92.     return modtext
  93.   end
  94.  
  95.   function debuglog ()
  96.     tpt.drawtext(20, 50,
  97.       "KSNS Debug: " ..
  98.       "\nkeycode: " .. lastkeyn ..
  99.       "\nmod: " .. lastmod .. " / " .. getmodtext(lastmod) ..
  100.       "\npressing: " .. tostring(pressing))
  101.   end
  102.  
  103.   tpt.register_step(debuglog)
  104. end
Add Comment
Please, Sign In to add comment