This is a read-only snapshot of the ComputerCraft forums,
taken in April 2020.
KeyBinding?
Started by ds84182, 20 March 2012 - 07:22 PMPosted 20 March 2012 - 08:22 PM
Is it possible to bind keys? Like a simple function EG: os.bindKey(key, function)
Posted 20 March 2012 - 08:23 PM
it is not, but it could be… i have to think it over.
Posted 20 March 2012 - 08:31 PM
What about coroutines? I would do it myself, but I can't use those pesky things.
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/>/>
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.:
If that is not what you meant, then can you describe a little example of what you want to achieve? :D/>/>
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/>/>
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
Posted 20 March 2012 - 10:54 PM
Thats what I ment. Is there any wait times? I'm have audio playing with the SuperNoteBlock that needs to be evenly spaced.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
Posted 20 March 2012 - 11:31 PM
"B" should be "b"
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
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!