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

Keybindings?

Started by ReBraLaCC, 20 August 2016 - 09:01 AM
ReBraLaCC #1
Posted 20 August 2016 - 11:01 AM
So i've been trying to get like ctrl+u and other keys to do things but the way i've tried it didn't work :(/>


while true do
 ev = {os.pullEvent()}
 if ev[1] == "key" then
   ev2 = {os.pullEvent()}
   if ev2[1] == "key" then
    if ev[2] == keys.leftCrtl and ev2[2] == keys.u then
     return true
    end
   end
 end
end

i hope that there is maybe a simpler way to do this and a working one :)/>

thanks
– ReBraLa
Bomb Bloke #2
Posted 20 August 2016 - 11:21 AM
if ev[2] == keys.leftCrtl and ev2[2] == keys.u then

Both can't be true at once.

You get one event per key pressed, in the order they're pressed. Under later versions of CC you also get "key_up" events indicating when keys are released. If you want to know whether multiple keys are held at the same time, then you'll need to record buttons of interest…

For eg:

local heldCtrl = false

while true do
	local ev = {os.pullEvent()}
	
	if ev[1] == "key" then
		if ev[2] == keys.leftCtrl then
			heldCtrl = true
		elseif heldCtrl and ev2[2] == keys.u then
			return true
		end
	elseif ev[1] == "key_up" and ev[2] == keys.leftCtrl then
		heldCtrl = false
	end
end
ReBraLaCC #3
Posted 20 August 2016 - 11:43 AM
….

Thank you very much! You only had a lil typo but i fixed it :)/> going to leave this here… thats what i need it for :)/>