waiting for button input

Discuss using and improving Lua and the Lua Player specific to the PSP.

Moderators: Shine, Insert_witty_name

Post Reply
the underminer
Posts: 123
Joined: Mon Oct 03, 2005 4:25 am
Location: Netherlands

waiting for button input

Post by the underminer »

Ever since I know lua I have been using this code to wait till a button is pressed, only to find out that it doesn't work!

Code: Select all

pad = nil
while pad == nil do
  System.sleep(100)
  pad = Controls.read()
end
If I add a print("hello") to my code after end my screen is filled with 10 hello's per second(I guess) although I don't press any buttons. This is pretty embarassing, so can anyone tell me how to do this?
Behold! The Underminer got hold of a PSP
matriculated
Posts: 31
Joined: Sat Mar 04, 2006 1:35 am

Post by matriculated »

pad will never = nil if you do a controls.read

do something like this:

Code: Select all

repeat
  System.sleep(100)
  pad = Controls.read()
until pad:buttons()
This loops until a button (or dpad) is pressed
the underminer
Posts: 123
Joined: Mon Oct 03, 2005 4:25 am
Location: Netherlands

Post by the underminer »

that doesn't work either
Behold! The Underminer got hold of a PSP
matriculated
Posts: 31
Joined: Sat Mar 04, 2006 1:35 am

Post by matriculated »

It does work because I use a slightly modified code in all my programs to pause for input.

Code: Select all

function pause()
  local pad
  repeat
    pad = Controls.read()
    screen.waitVblankStart(5)
  until pad:start()
end
romero126
Posts: 200
Joined: Sat Dec 24, 2005 2:42 pm

Post by romero126 »

Code: Select all

-- Coded by romero126 
GlassEyeInput = { 
   ["KeyList"] = { } 
} 

table.insert(GlassEyeInput["KeyList"], {Controls.selectMask, "Select", nil}) 
table.insert(GlassEyeInput["KeyList"], {Controls.startMask, "Start", nil}) 

table.insert(GlassEyeInput["KeyList"], {Controls.upMask, "Up", nil}) 
table.insert(GlassEyeInput["KeyList"], {Controls.rightMask, "Right", nil}) 
table.insert(GlassEyeInput["KeyList"], {Controls.downMask, "Down", nil}) 
table.insert(GlassEyeInput["KeyList"], {Controls.leftMask, "Left", nil}) 

table.insert(GlassEyeInput["KeyList"], {Controls.ltriggerMask, "LTrigger", nil}) 
table.insert(GlassEyeInput["KeyList"], {Controls.rtriggerMask, "RTrigger", nil}) 

table.insert(GlassEyeInput["KeyList"], {Controls.triangleMask, "Triangle", nil}) 
table.insert(GlassEyeInput["KeyList"], {Controls.circleMask, "Circle", nil}) 
table.insert(GlassEyeInput["KeyList"], {Controls.crossMask, "Cross", nil}) 
table.insert(GlassEyeInput["KeyList"], {Controls.squareMask, "Square", nil}) 

table.insert(GlassEyeInput["KeyList"], {Controls.homeMask, "Home", nil}) 
table.insert(GlassEyeInput["KeyList"], {Controls.holdMask, "Hold", nil}) 
table.insert(GlassEyeInput["KeyList"], {Controls.noteMask, "Note", nil}) 

function GlassEyeInput:CheckInput(key) 
   result = nil 
   for k, v in GlassEyeInput["KeyList"] do 
      if (key:buttons() & v[1] > 0) then 
         if (not v[3]) then 
            v[3] = 1 
            if (not result) then result = { } end 
            result[v[2]] = 1 
         end 
      else 
         if (v[3]) then 
            if (not result) then result = { } end 
            result[v[2]] = 0 
            v[3] = nil 
         end 
      end 
   end 
   return result 
end 

Code: Select all

-- Coded by romero126 
if (GlassEyeInput) then 
   local check = GlassEyeInput:CheckInput(key) 
   if (check) then 
      for k,v in check do 
         if (k == "Start") and (v == 0) then 
            print("You lifted your finger on the start key") 
         end 
         if (k == "Start") and (v == 1) then 
            print("You pressed your finger on the start key") 
         end 
      end 
   end 
end 
Enjoy!
romero126
Posts: 200
Joined: Sat Dec 24, 2005 2:42 pm

Post by romero126 »

cheap input is also done by
key = Controls.read()
while true do

if (key != Controls.read())
key = Controls.read()
-- do stuff here
end

end
Post Reply