This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
ds84182's profile picture

KeyBinding?

Started by ds84182, 20 March 2012 - 07:22 PM
ds84182 #1
Posted 20 March 2012 - 08:22 PM
Is it possible to bind keys? Like a simple function EG: os.bindKey(key, function)
Liraal #2
Posted 20 March 2012 - 08:23 PM
it is not, but it could be… i have to think it over.
ds84182 #3
Posted 20 March 2012 - 08:31 PM
What about coroutines? I would do it myself, but I can't use those pesky things.
Liraal #4
Posted 20 March 2012 - 08:34 PM
it's certainly possible, i think i can do it tomorrow, but today it's already bedtime :D/>/>
Espen #5
Posted 20 March 2012 - 09:30 PM
@ds84182:
You can have an event loop, within which you can listen for keys being pressed, e.g.:

while true do
  local event, pressed = os.pullEvent()

  -- Prints if Enter or Left Shift have been pressed.
  if event == "key" and pressed == 28 then print("Enter Key pressed") end
  if event == "key" and pressed == 42 then print("Left Shift Key pressed") end

  -- Prints every entered character.
  if event == "char" then
	print( pressed )
  end

  if event == "char" and pressed == "q" then break end  -- Exit Program
end

If that is not what you meant, then can you describe a little example of what you want to achieve? :D/>/>
Liraal #6
Posted 20 March 2012 - 09:37 PM
i think he means something more like:

local keys={59, 60}
local functions={function () print("abc") end, function() term.clear() end}
function exists(tab,a)
for i=1, #tab, 1 do
if tab[i]==a then return true end
end
return false
end
function num(tab,a)
for i=1, #tab, 1 do
if tab[i]==a then return i end
end
return false
end
function waitForKey()
while true do
local a,b=os.pullEvent("key")
if exists(keys,:D/>/> then
local tmp=functions[num(keys,:)/>/>]
tmp()
else
os.queueEvent(a,:)/>/>
end
end
end
--splitting the actual thread
while true do
parallel.waitForAny(
  function()
   waitForKey()
  end,
  function()
   os.run( {}, "rom/programs/shell" )
  end
)
end
ds84182 #7
Posted 20 March 2012 - 10:54 PM
i think he means something more like:

local keys={59, 60}
local functions={function () print("abc") end, function() term.clear() end}
function exists(tab,a)
for i=1, #tab, 1 do
if tab[i]==a then return true end
end
return false
end
function num(tab,a)
for i=1, #tab, 1 do
if tab[i]==a then return i end
end
return false
end
function waitForKey()
while true do
local a,b=os.pullEvent("key")
if exists(keys,:D/>/> then
local tmp=functions[num(keys,:)/>/>]
tmp()
else
os.queueEvent(a,:)/>/>
end
end
end
--splitting the actual thread
while true do
parallel.waitForAny(
  function()
   waitForKey()
  end,
  function()
   os.run( {}, "rom/programs/shell" )
  end
)
end
Thats what I ment. Is there any wait times? I'm have audio playing with the SuperNoteBlock that needs to be evenly spaced.
ds84182 #8
Posted 20 March 2012 - 11:31 PM
"B" should be "b"
Liraal #9
Posted 21 March 2012 - 06:21 AM
oh sorry, must have mistyped, and the code should read every press of the keys given (in this case, F1 and F2) as it occurs
ds84182 #10
Posted 21 March 2012 - 09:38 PM
I'll soon post a video of my MusicOS! Without you, my pause button and menus wouldn't work!