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

keypress comands

Started by Bladedemon70, 02 March 2012 - 03:05 AM
Bladedemon70 #1
Posted 02 March 2012 - 04:05 AM
Ok i looked around and seen os.pullevent()
and i was going to incorporate it but can't quite grasp it i guess since im trying to make one key go to a shutdown function and another key go back

any help is welcomed….
Casper7526 #2
Posted 02 March 2012 - 04:11 AM
http://www.computercraft.info/forums2/index.php?/topic/13-interactive-computercraft-tutorial-in-game/

Whole section on events if you interested :unsure:/>/>
Bladedemon70 #3
Posted 02 March 2012 - 04:12 AM
Ok overlooked that ill check and then bring question i have to here….

Thanks Casper
Bladedemon70 #4
Posted 02 March 2012 - 06:03 AM
Hey so looked over it and i get it and wrote it in but when i got into the program i press the key but it seems to just stop the program…..

heres the code: *just a countdown then door opens I making it to expand my coding knowledge
Spoiler

--Countdown to open door
-- Variables
--Door Variables
--Username
Username = "Admin"
--Password
Password = "Password1"
--Countdown Variables
--Number Variables
number = {}
number[0] = colors.cyan + colors.magenta + colors.yellow + colors.red + colors.green + colors.blue
number[1] = colors.magenta + colors.blue
number[2] = colors.cyan + colors.magenta + colors.black + colors.red + colors.green
number[3] = colors.cyan + colors.magenta + colors.black + colors.blue + colors.green
number[4] = colors.magenta + colors.yellow + colors.black + colors. blue
number[5] = colors.cyan + colors.yellow + colors.black + colors.blue + colors.green
number[6] = colors.cyan + colors.yellow + colors.black + colors.red + colors.green + colors.blue
number[7] = colors.cyan + colors.magenta + colors.blue
number[8] = colors.cyan + colors.magenta + colors.yellow + colors.black + colors.red + colors.green + colors.blue
number[9] = colors.cyan + colors.magenta + colors.yellow + colors.black + colors.green + colors.blue
--Functions
--clear all function
function clearall()
term.clear()
term.setCursorPos(1,1)
end
--Loadbar function
--(Thanks to xgaarocha)
--Link: http://www.computercraft.info/forums2/index.php?/topic/180-gaas-useful-functions/page__hl__loading__fromsearch__1
function loadbar(s, char, size)
	-- Define parameters
	s = s or 10
	size = size or 20
	char = char or "%"
	local xpos,ypos = term.getCursorPos()
	local delay = s/size
	local start = 1
	while start < size  do
	term.setCursorPos(start, ypos)
	print (char)
	sleep(delay)
	start = start + 1
end
end
--countdown function
function countdown()
for x = 5,0 do
  redstone.setBundledOutput("back", number[x])
  sleep(2)
end
end
--Login screen making function
function Loginscreen()
term.setCursorPos(1,1)
print ("		 (@|")
term.setCursorPos(1,2)
print (" ,,	  ,)|_________________________________")
term.setCursorPos(1,3)
print ("//\\8@8@8@8 / _   B L A D E D E M O N 7 0   _ \ ")
term.setCursorPos(1,4)
print ("\\//8@8@8@8 \_________________________________/ ")
term.setCursorPos(1,5)
print (" ``	  `)|")
term.setCursorPos(1,6)
print ("		 (@|")
term.setCursorPos(12,7)
print ("+----------------------------+")
term.setCursorPos(12,8)
print ("|-----------Login:-----------|")
term.setCursorPos(12,9)
print ("+----------------------------+")
term.setCursorPos(12,10)
print ("| Username:				  |")
term.setCursorPos(12,11)
print ("+----------------------------+")
term.setCursorPos(12,12)
print ("| Password:				  |")
term.setCursorPos(12,13)
print ("+----------------------------+")
end
--Shutdown function
function Shutdown()
sleep(.5)
clearall()
print ("Shutting down...")
sleep(.5)
print ("Goodbye.")
sleep(.5)
os.shutdown()
end
--goto_mainmenu function
function goto_mainmenu()
sleep(.5)
mainmenu()
end
--Activate_Count function
function Activate_Count()
print ("Please direct your eyes to the Countdown display.")
print ("When the countdown hits 0, the door will open and you may proceed.")
print ("Starting now...")
countdown()
sleep(.5)
term.setBundledOutput("right", true)
sleep(5)
print ("Goodbye")
term.setBundledOutput("right", false)
sleep(.5)
os.reboot()
end
-- Quit_back function
function Quit_back()
sleep(.5)
clearall()
setCursorPos(1,11)
print ("Would you like to quit?")
print ("Press 'Q' to quit or 'B' to go back.")
event, param1, param2 = os.pullEvent()
if event == "key" and param1 == 113 then Shutdown() end
if event == "key" and param2 == 98 then goto_mainmenu() end
end
--Mainmenu function
function mainmenu()
clearall()
term.setCursorPos(12,1)
print ("+----------------------------+")
term.setCursorPos(12,2)
print ("|----------Blade.Os----------|")
term.setCursorPos(12,3)
print ("+----------------------------+")
term.setCursorPos(12,6)
print ("Welcome to the Terminal Control Panel.")
term.setCursorPos(1,8)
print ("Would you like to open the door?")
print ("Press 'Y' for Yes and 'N' for No...")
event, param1, param2 = os.pullEvent()
if event == "key" and param1 == 121 then Activate_Count() end --Y for yes 121 = y
if event == "key" and param2 == 110 then Quit_back() end --N for no 110 = n
end
--Main Program
clearall()
loadbar(s, char, size)
clearall()
sleep(.5)
Loginscreen()
term.setCursorPos(23,10)
local input = read()
if input == Username then
  term.setCursorPos(23,12)
  local input = read()
   if input == Password then
	sleep(.5)
	mainmenu()
   else -- incorrect password
	sleep(.5)
	clearall()
	term.setCursorPos(1,1)
	print ("Incorrect Password.")
	sleep(.5)
	print ("Shutting down....")
	sleep(1)
	os.shutdown()
   end
else
  sleep(.5)
  clearall()
  term.setCursorPos(1,1)
  print ("System Crash...")
  sleep(.5)
  print ("Rebooting...")
  sleep(1)
  os.reboot()
end

Anyother fixes and condesing are welcomed because I'm here to learn and if there is a shotter way to do stuff then i want to know

Finally, thanks a head of time to anyone that helps (Ill do my best to thankyou in my replies anyway)
Casper7526 #5
Posted 02 March 2012 - 06:14 AM
event, param1, param2 = os.pullEvent()
if event == "key" and param1 == 121 then Activate_Count() end
if event == "key" and param2 == 110 then Quit_back() end

Not bad for a first try at all.
The param2 variable is only used with events that have more than 1 variable.
The key event only returns 1 variable so you'll only ever use param1

And if you doing something simple like letters it's prolly easier to just use the "char" event


if event == "char" and param1 == "y" then asdasdasd
if event == "char" and param1 == "n" then asdasdasd

you could also make sure case isn't an issue by doing

if event == "char" and string.lower(param1) == "y" – this way they can type Y or y

Also when using the pull event, most of the time you'll want to place it within a loop so that if any other event occurs that you don't want, it will just go back to the top

To use the char event you would have to have a loop though, because the key event is fired then the char event
Bladedemon70 #6
Posted 02 March 2012 - 02:22 PM
Ok well i added the stuff you pointed out but when i get to the point where it asks for you to press y or n to open the door and it stops and nothing happens when i press the right key

do you have to take out the param2 for the line:

event, param1, param2 = os.pullEvent()
Bladedemon70 #7
Posted 03 March 2012 - 01:58 AM
Hey still having problems and i replaced the things you said casper but it still doesnt work when i press a key

do i need the param2 in the line:

event, param1, param2 = os.pullEvent()
Casper7526 #8
Posted 03 March 2012 - 02:03 AM
param2 is just used when an event has 2 parameters. So technically unless your using an event that requires 2 parameters (rednet_message, http stuff) you only ever need param1.

Post your updated code and someone here will look it over though :unsure:/>/>
MysticT #9
Posted 03 March 2012 - 02:07 AM
I think the key codes are wrong. This are the codes for all letters:
SpoilerA = 30
B = 48
C = 46
D = 32
E = 18
F = 33
G = 34
H = 35
I = 23
J = 36
K = 37
L = 38
M = 50
N = 49
O = 24
P = 25
Q = 16
R = 19
S = 31
T = 20
U = 22
V = 47
W = 17
X = 45
Y = 21
Z = 44
Try changing them, if that doesn't work, post the updated code and we'll take a look at it.
Bladedemon70 #10
Posted 03 March 2012 - 03:03 AM
Mystic yea probably got them off some shotty website and stuff but I also ended up changing them to "char"
Bladedemon70 #11
Posted 03 March 2012 - 03:07 AM
Heres the updated code:

(should have posted it earlier)

Spoiler

--Countdown to open door
-- Variables
--Door Variables
--Username
Username = "Admin"
--Password
Password = "Password1"
--Countdown Variables
--Number Variables
number = {}
number[0] = colors.cyan + colors.magenta + colors.yellow + colors.red + colors.green + colors.blue
number[1] = colors.magenta + colors.blue
number[2] = colors.cyan + colors.magenta + colors.black + colors.red + colors.green
number[3] = colors.cyan + colors.magenta + colors.black + colors.blue + colors.green
number[4] = colors.magenta + colors.yellow + colors.black + colors. blue
number[5] = colors.cyan + colors.yellow + colors.black + colors.blue + colors.green
number[6] = colors.cyan + colors.yellow + colors.black + colors.red + colors.green + colors.blue
number[7] = colors.cyan + colors.magenta + colors.blue
number[8] = colors.cyan + colors.magenta + colors.yellow + colors.black + colors.red + colors.green + colors.blue
number[9] = colors.cyan + colors.magenta + colors.yellow + colors.black + colors.green + colors.blue
--Functions
--clear all function
function clearall()
term.clear()
term.setCursorPos(1,1)
end
--Loadbar function
--(Thanks to xgaarocha)
--Link: http://www.computercraft.info/forums2/index.php?/topic/180-gaas-useful-functions/page__hl__loading__fromsearch__1
function loadbar(s, char, size)
    -- Define parameters
    s = s or 10
    size = size or 20
    char = char or "%"
    local xpos,ypos = term.getCursorPos()
    local delay = s/size
    local start = 1
    while start < size  do
    term.setCursorPos(start, ypos)
    print (char)
    sleep(delay)
    start = start + 1
end
end
--countdown function
function countdown()
for x = 5,0 do
  redstone.setBundledOutput("back", number[x])
  sleep(2)
end
end
--Login screen making function
function Loginscreen()
term.setCursorPos(1,1)
print ("		 (@|")
term.setCursorPos(1,2)
print (" ,,	  ,)|_________________________________")
term.setCursorPos(1,3)
print ("//\\8@8@8@8 / _   B L A D E D E M O N 7 0   _ \ ")
term.setCursorPos(1,4)
print ("\\//8@8@8@8 \_________________________________/ ")
term.setCursorPos(1,5)
print (" ``	  `)|")
term.setCursorPos(1,6)
print ("		 (@|")
term.setCursorPos(12,7)
print ("+----------------------------+")
term.setCursorPos(12,8)
print ("|-----------Login:-----------|")
term.setCursorPos(12,9)
print ("+----------------------------+")
term.setCursorPos(12,10)
print ("| Username:				  |")
term.setCursorPos(12,11)
print ("+----------------------------+")
term.setCursorPos(12,12)
print ("| Password:				  |")
term.setCursorPos(12,13)
print ("+----------------------------+")
end
--Shutdown function
function Shutdown()
sleep(.5)
clearall()
print ("Shutting down...")
sleep(.5)
print ("Goodbye.")
sleep(.5)
os.shutdown()
end
--goto_mainmenu function
function goto_mainmenu()
sleep(.5)
mainmenu()
end
--Activate_Count function
function Activate_Count()
print ("Please direct your eyes to the Countdown display.")
print ("When the countdown hits 0, the door will open and you may proceed.")
print ("Starting now...")
countdown()
sleep(.5)
term.setBundledOutput("right", true)
sleep(5)
print ("Goodbye")
term.setBundledOutput("right", false)
sleep(.5)
os.reboot()
end
-- Quit_back function
function Quit_back()
sleep(.5)
clearall()
setCursorPos(1,11)
print ("Would you like to quit?")
print ("Press 'Q' to quit or 'B' to go back.")
event, param1 = os.pullEvent()
if event == "char" and string.lower(param1) == "q" then Shutdown() end
if event == "char" and string.lower(param1) == "b" then goto_mainmenu() end
end
--Mainmenu function
function mainmenu()
clearall()
term.setCursorPos(12,1)
print ("+----------------------------+")
term.setCursorPos(12,2)
print ("|----------Blade.Os----------|")
term.setCursorPos(12,3)
print ("+----------------------------+")
term.setCursorPos(1,6)
print ("Welcome to the Terminal Control Panel.")
term.setCursorPos(1,8)
print ("Would you like to open the door?")
print ("Press 'Y' for Yes and 'N' for No...")
event, param1 = os.pullEvent()
if event == "char" and string.lower(param1) == "y" then Activate_Count() end --Y for yes 121 = y
if event == "char" and string.lower(param1) == "n" then Quit_back() end --N for no 110 = n
end
--Main Program
clearall()
loadbar(s, char, size)
clearall()
sleep(.5)
Loginscreen()
term.setCursorPos(23,10)
local input = read()
if input == Username then
  term.setCursorPos(23,12)
  local input = read()
   if input == Password then
    sleep(.5)
    mainmenu()
   else -- incorrect password
    sleep(.5)
    clearall()
    term.setCursorPos(1,1)
    print ("Incorrect Password.")
    sleep(.5)
    print ("Shutting down....")
    sleep(1)
    os.shutdown()
   end
else
  sleep(.5)
  clearall()
  term.setCursorPos(1,1)
  print ("System Crash...")
  sleep(.5)
  print ("Rebooting...")
  sleep(1)
  os.reboot()
end
Casper7526 #12
Posted 03 March 2012 - 03:58 AM
event, param1 = os.pullEvent()

change that too

event, param1 = os.pullEvent("char")
Bladedemon70 #13
Posted 03 March 2012 - 05:03 AM
Ok well that is fixed now heres the code and now having another problem and its with the countdown area

Also my countdown isn't starting either…

Heres the code:
Spoiler

--Countdown to open door
-- Variables
--Door Variables
--Username
Username = "Admin"
--Password
Password = "Password1"
--Countdown Variables
--Number Variables
number = {}
number[0] = colors.cyan + colors.magenta + colors.yellow + colors.red + colors.green + colors.blue
number[1] = colors.magenta + colors.blue
number[2] = colors.cyan + colors.magenta + colors.black + colors.red + colors.green
number[3] = colors.cyan + colors.magenta + colors.black + colors.blue + colors.green
number[4] = colors.magenta + colors.yellow + colors.black + colors. blue
number[5] = colors.cyan + colors.yellow + colors.black + colors.blue + colors.green
number[6] = colors.cyan + colors.yellow + colors.black + colors.red + colors.green + colors.blue
number[7] = colors.cyan + colors.magenta + colors.blue
number[8] = colors.cyan + colors.magenta + colors.yellow + colors.black + colors.red + colors.green + colors.blue
number[9] = colors.cyan + colors.magenta + colors.yellow + colors.black + colors.green + colors.blue
--Functions
--clear all function
function clearall()
term.clear()
term.setCursorPos(1,1)
end
--Loadbar function
--(Thanks to xgaarocha)
--Link: http://www.computercraft.info/forums2/index.php?/topic/180-gaas-useful-functions/page__hl__loading__fromsearch__1
function loadbar(s, char, size)
    -- Define parameters
    s = s or 10
    size = size or 20
    char = char or "%"
    local xpos,ypos = term.getCursorPos()
    local delay = s/size
    local start = 1
    while start < size  do
    term.setCursorPos(start, ypos)
    print (char)
    sleep(delay)
    start = start + 1
end
end
--countdown function
function countdown()
for x = 5,0 do
  redstone.setBundledOutput("back", number[x])
  sleep(2)
end
end
--Login screen making function
function Loginscreen()
term.setCursorPos(1,1)
print ("		 (@|")
term.setCursorPos(1,2)
print (" ,,	  ,)|_________________________________")
term.setCursorPos(1,3)
print ("//8@8@8@8 / _   B L A D E D E M O N 7 0   _  ")
term.setCursorPos(1,4)
print ("//8@8@8@8 _________________________________/ ")
term.setCursorPos(1,5)
print (" ``	  `)|")
term.setCursorPos(1,6)
print ("		 (@|")
term.setCursorPos(12,7)
print ("+----------------------------+")
term.setCursorPos(12,8)
print ("|-----------Login:-----------|")
term.setCursorPos(12,9)
print ("+----------------------------+")
term.setCursorPos(12,10)
print ("| Username:				  |")
term.setCursorPos(12,11)
print ("+----------------------------+")
term.setCursorPos(12,12)
print ("| Password:				  |")
term.setCursorPos(12,13)
print ("+----------------------------+")
end
--Shutdown function
function Shutdown()
sleep(.5)
clearall()
print ("Shutting down...")
sleep(.5)
print ("Goodbye.")
sleep(.5)
os.shutdown()
end
--goto_mainmenu function
function goto_mainmenu()
sleep(.5)
mainmenu()
end
--Activate_Count function
function Activate_Count()
print ("Please direct your eyes to the Countdown display.")
sleep(.5)
print ("When the countdown hits 0, the door will open and you may proceed.")
sleep(.5)
print ("Starting now...")
sleep(1)
countdown()
sleep(7)
print ("Opening...")
sleep(.5)
  redstone.setBundledOutput("back", false)
  redstone.setBundledOutput("right", true)
sleep(5)
print ("Goodbye")
  redstone.setBundledOutput("right", false)
sleep(.5)
os.reboot()
end
-- Quit_back function
function Quit_back()
sleep(.5)
clearall()
setCursorPos(1,11)
print ("Would you like to quit?")
print ("Press 'Q' to quit or 'B' to go back.")
event, param1 = os.pullEvent("char")
if event == "char" and string.lower(param1) == "q" then Shutdown() end
if event == "char" and string.lower(param1) == "b" then goto_mainmenu() end
end
--Mainmenu function
function mainmenu()
clearall()
term.setCursorPos(12,1)
print ("+----------------------------+")
term.setCursorPos(12,2)
print ("|----------Blade.Os----------|")
term.setCursorPos(12,3)
print ("+----------------------------+")
term.setCursorPos(1,6)
print ("Welcome to the Terminal Control Panel.")
term.setCursorPos(1,8)
print ("Would you like to open the door?")
print ("Press 'Y' for Yes and 'N' for No...")
event, param1 = os.pullEvent("char")
if event == "char" and string.lower(param1) == "y" then Activate_Count() end --Y for yes 121 = y
if event == "char" and string.lower(param1) == "n" then Quit_back() end --N for no 110 = n
end
--Main Program
clearall()
loadbar(s, char, size)
clearall()
sleep(.5)
Loginscreen()
term.setCursorPos(23,10)
local input = read()
if input == Username then
  term.setCursorPos(23,12)
  local input = read()
   if input == Password then
    sleep(.5)
    mainmenu()
   else -- incorrect password
    sleep(.5)
    clearall()
    term.setCursorPos(1,1)
    print ("Incorrect Password.")
    sleep(.5)
    print ("Shutting down....")
    sleep(1)
    os.shutdown()
   end
else
  sleep(.5)
  clearall()
  term.setCursorPos(1,1)
  print ("System Crash...")
  sleep(.5)
  print ("Rebooting...")
  sleep(1)
  os.reboot()
end

Problem/code segment:
SpoilerThe error is:
Countdown_doorlock.lua:117: bad argument: int expected, got boolean

Spoiler

--Activate_Count function
function Activate_Count()
print ("Please direct your eyes to the Countdown display.")
sleep(.5)
print ("When the countdown hits 0, the door will open and you may proceed.")
sleep(.5)
print ("Starting now...")
sleep(1)
countdown()
sleep(7)
print ("Opening...")
sleep(.5)
  redstone.setBundledOutput("back", false)
  redstone.setBundledOutput("right", true)
sleep(5)
print ("Goodbye")
  redstone.setBundledOutput("right", false)
sleep(.5)
os.reboot()
end

btw thanks a bunch Casper your a wonderful help
Casper7526 #14
Posted 03 March 2012 - 05:07 AM
Your using setBundledOutput which needs a number as it's 2nd parameter


redstone.setBundledOutput("back", false) – instead of false use 0 (this will turn off all output)

redstone.setBundledOutput("right",true)


If you are just using normal redstone do
redstone.setOutput("right", true)
Bladedemon70 #15
Posted 03 March 2012 - 05:54 AM
Ok thanks Casper that worked…
The only thing is the countdown to the door opening

here the function for it with then table of numbers:
Spoiler

--Countdown Variables
--Number Variables
number = {}
number[0] = colors.cyan + colors.magenta + colors.yellow + colors.red + colors.green + colors.blue
number[1] = colors.magenta + colors.blue
number[2] = colors.cyan + colors.magenta + colors.black + colors.red + colors.green
number[3] = colors.cyan + colors.magenta + colors.black + colors.blue + colors.green
number[4] = colors.magenta + colors.yellow + colors.black + colors. blue
number[5] = colors.cyan + colors.yellow + colors.black + colors.blue + colors.green
number[6] = colors.cyan + colors.yellow + colors.black + colors.red + colors.green + colors.blue
number[7] = colors.cyan + colors.magenta + colors.blue
number[8] = colors.cyan + colors.magenta + colors.yellow + colors.black + colors.red + colors.green + colors.blue
number[9] = colors.cyan + colors.magenta + colors.yellow + colors.black + colors.green + colors.blue


--countdown function
function countdown()
for x = 5,0 do
  redstone.setBundledOutput("back", number[x])
  sleep(.5)
end
end
Advert #16
Posted 03 March 2012 - 06:40 AM
Try this:

for x = 5, 0, -1 do

You might need to specify the step (the -1, in this case) when counting down in a for loop.

I would also recommend using 0.5 for the number, instead of .5, even if just for the readability.
Bladedemon70 #17
Posted 03 March 2012 - 07:02 PM
Ok thanks Advert