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

Use os.pullEvent With Variables

Started by hbomb79, 30 June 2014 - 06:51 AM
hbomb79 #1
Posted 30 June 2014 - 08:51 AM
I have a program that reads Variables from a file but when i press the key nothing happens, it only stopped woring after i put the variabled in the code:


if key == keys.confighotkey then
	   clear()
    config()
	 elseif key == keys.shutdownhotkey then
	  os.shutdown()
	 elseif key == keys.reboothotkey then
	  os.reboot()
	 elseif key == keys.BIOShotkey then
	  error()
	 end

Of course the variable names are
- confighotkey
- shutdownhotkey
- reboothotkey
- BIOShotkey



Thanks In Advance!
theoriginalbit #2
Posted 30 June 2014 - 09:30 AM
where do you define the table keys with which you have the variables

- confighotkey
- shutdownhotkey
- reboothotkey
- BIOShotkey
Edited on 30 June 2014 - 07:31 AM
Bomb Bloke #3
Posted 30 June 2014 - 09:39 AM
For that matter, where is "key" defined?
hbomb79 #4
Posted 30 June 2014 - 09:44 AM
Full Code Is Here:


function initiate()
  if fs.exists ("config") then
	local f = fs.open("config", "r")
timecount = f.readLine()
confighotkey = f.readLine()
shutdownhotkey = f.readLine()
reboothotkey = f.readLine()
BIOShotkey = f.readLine()
f.close()
  else
  end
end

function config()
  con = 2
  clear()
  print "You Have Entered Config, Select What You Would Like To Edit"
  print""
  print "[1] Launch Countdown"
  print "[2] Adjust Hot-keys"
  print "[3] Save And Return"
  print "[4] Back To Software"
  write "Option: "
  option = read()
  if option == "1" then
	clear()
print "How Long Do You Want The Launch Countdown To Last"
write "Amount (In Seconds): "
timecount = read()
	config()
  elseif option == "2" then
	clear()
print "[1] Configuration Enter Hot-key"
print "[2] Shut-down Hot-key"
print "[3] Reboot Hot-key"
print "[4] Crash Hot-key"
print "[5] Back"
write "Option: "
hotopt = read()
   if hotopt == "1" then
	 clear()
	  print "New Key For Config"
   write"New Key: "
   confighotkey = read()
   config()
   elseif hotopt == "2" then
	 print "New Key For ShutDown"
  write"New Key: "
  shutdownhotkey = read()
  config()
   elseif hotopt == "3" then
	 print "New Key For Reboot"
  write"New Key: "
  reboothotkey = read()
  config()
   elseif hotopt == "4" then
	 print "New Key For BIOS"
  write"New Key: "
  BIOShotkey = read()
  config()
   elseif hotopt == "5" then
	 config()
   else
	 error()
   end
  elseif option == "3" then
	print "Saving, Please Wait"
	local f = fs.open("config", "w")
f.writeLine(timecount)
f.writeLine(confighotkey)
f.writeLine(shutdownhotkey)
f.writeLine(reboothotkey)
f.writeLine(BIOShotkey)
f.close()
local f = fs.open("pcstate", "w")
f.writeLine("Yes")
f.close()
sleep(5)
print "Saving Completed, Computer Restart Required..."
print "Press Any Key To Restart"
os.pullEvent("key")
os.reboot()
  elseif option == "4" then
	os.reboot()
  else
  end
end
function drawStart()
print "Coded By hbomb_79"
print "Loading Config"
sleep(1)
rs.setOutput("bottom", true)
initiate()
sleep(1)
rs.setOutput("bottom", false)
sleep(1)
rs.setOutput("bottom", true)
sleep(1)
rs.setOutput("bottom", false)
sleep(1)
rs.setOutput("bottom", true)
sleep(1)
print "Config Loaded"
sleep(1)
clear()
print "Coded By hbomb_79"
print""
print ("Hotkeys: Config-"..confighotkey.." Shutdown-"..shutdownhotkey.." Reboot-"..reboothotkey.." BIOS-"..BIOShotkey)
print "Insert Both Launch Cards To Continue With Launch!"
end
function clear()
  term.setCursorPos(1,1)
  term.clear()
end
local sides = { "top", "bottom", "left", "right", "front", "back" }
r = "1"
function ejectdisks()
for i = 1, #sides do
  if peripheral.isPresent(sides[i]) then
	if peripheral.getType(sides[i]) == "drive" then
	  print("Ejecting Disk From "..peripheral.getType(sides[i]).." On Side "..sides[i])
   disk.eject(sides[i])
	end
   end
  end
end
function error()
  clear()

  print "Something Went Wrong And The Computer Needs To Shutdown..."
  sleep(10)
  os.shutdown()
end


--Defining Local Variables
local keyFile = "Encryption"
local keyContents = "12345"
clear()
i = 1
for i = 1, #sides do
  if peripheral.isPresent(sides[i]) then
	if peripheral.getType(sides[i]) == "drive" then
	  print("A "..peripheral.getType(sides[i]).." Is On "..sides[i])
   disk.eject(sides[i])
   sleep(1)
	end
  end
end
function start()
   function counter()
   clear()
	 print "Counter Started!"
	 for x = timecount,1, -1 do
			oldX, oldY = term.getCursorPos()
			term.setCursorPos(1,1)
			term.clearLine()
	print(x)
	rs.setOutput("top",true)
	term.setCursorPos(oldX, oldY)
	sleep(1)
  end
   end
  
   function diskremove()
	 term.setCursorPos(1,2)
  rs.setOutput("top", true)
  print "Remove A Disk To Abort Launch!"
  os.pullEvent("disk_eject")
  print "Launch Aborted"
  sleep(1)
  remove = true
   end
  
parallel.waitForAny(counter, diskremove)
	print "Parallel Wait Completed"
if remove == true then
   print "Launch Aborted"
   print "Ejecting Disks Out Of Drives!"
	  ejectdisks()
   rs.setOutput("top", false)
   clear()
   drawStart()
else
	  print "Launching!"
   rs.setOutput("back", true)
   sleep(5)
   rs.setOutput("back", false)
   ejectdisks()
   clear()
   drawStart()
end
end
drawStart()
while true do
--Get Raw Event Data
local rawEvent = {coroutine.yield()}
	local event = rawEvent[1]
	if event == "disk" then
local side = rawEvent[2]
  if r == "1" then
	   local filedisk1 = "/"..disk.getMountPath(side).."/"..keyFile
	   filedisk1 = fs.open(filedisk1,"r")
		 if filedisk1 then
		   file = filedisk1.readAll()
			if file == keyContents then
			  print("KeyCard 1 Accepted!")
	 r = "2"
			else
			 print("Invalid keyfile")
			end
		 else
		  -- Guess the lockfile's not there
		  print("Keyfile not found")
	rs.setOutput("back",true)
	os.startTimer(2)
		 end
  elseif r == "2" then
	   local filedisk2 = "/"..disk.getMountPath(side).."/"..keyFile
	   filedisk2 = fs.open(filedisk2,"r")
		 if filedisk2 then
		   file = filedisk2.readAll()
			if file == keyContents then
			  print("KeyCard 2 Accepted!")
	 print "Keycard Are Both Inserted, Countdown Started.."
	 rs.setOutput("bottom", false)
	 start()
			else
			 print("Invalid keyfile")
			end
		 else
		  -- Guess the lockfile's not there
		  print("Keyfile not found")
		 end
  elseif r == "3" then
	print "Keycards Are Both Inserted And Match, Countdown Started..."
	start()
	sleep(1)
  else
	  print "Unknown Event Occured..."
  end

	elseif event == "timer" then	
		-- This follows the timer set up in the lock checking code.
		-- Unset the door signal.
		rs.setOutput("back",false)
	elseif event == "terminate" then
		-- Prevents Ctrl-T (It would be ignored otherwise anyways, but...)
		print("[Termination Intercepted!]")
	elseif event == "disk_eject" then
   print "Disk Removed!"
   if r == "1" then
   elseif r == "2" then
	 r = "1"
   elseif r == "3" then end
elseif event == "key" then
   local key = rawEvent[2]
   if con ~= 2 then
	 if key == keys.confighotkey then
	   clear()
	config()
	 elseif key == keys.shutdownhotkey then
	  os.shutdown()
	 elseif key == keys.reboothotkey then
	  os.reboot()
	 elseif key == keys.BIOShotkey then
	  error()
	 end
  end
	end

  

end

Edited on 30 June 2014 - 08:07 AM
theoriginalbit #5
Posted 30 June 2014 - 09:49 AM
okay so if you do this in the initiate function

timecount = f.readLine()
confighotkey = f.readLine()
shutdownhotkey = f.readLine()
reboothotkey = f.readLine()
BIOShotkey = f.readLine()
then why are you trying to access them via a 'keys' table?

it should just be

if key == confighotkey then
  clear()
  config()
elseif key == shutdownhotkey then
  os.shutdown()
elseif key == reboothotkey then
  os.reboot()
elseif key == BIOShotkey then
  error()
end
Edited on 30 June 2014 - 07:49 AM
hbomb79 #6
Posted 30 June 2014 - 09:58 AM
When using that code, pressing the keys does nothing
Edited on 30 June 2014 - 07:59 AM
Bomb Bloke #7
Posted 30 June 2014 - 10:30 AM
That'd be because you're pulling key events, which return numeric values. Anything you read from a file comes in as a string.

Try this:

timecount = tonumber(f.readLine())
confighotkey = tonumber(f.readLine())
shutdownhotkey = tonumber(f.readLine())
reboothotkey = tonumber(f.readLine())
BIOShotkey = tonumber(f.readLine())

Failing that, what's in your config file?
hbomb79 #8
Posted 30 June 2014 - 10:39 AM
The config files is this:

30
g
s
r
b
theoriginalbit #9
Posted 30 June 2014 - 10:45 AM
ah. then you want the char event. the key event returns the id of the key pressed, not the character of said key. as such it would be better if you were to support key codes instead of characters, which would require a change from 'g' in the config to 34 as well as the others of course, the image in the key event wiki page shows the key codes of each key.
hbomb79 #10
Posted 30 June 2014 - 10:59 AM
I have a config program/function in the program, people dont know the code, will what bombbloke said work?? I dont want to have to enter random numbers when changing settings of the hotkeys….
theoriginalbit #11
Posted 30 June 2014 - 11:02 AM
no what bomb bloke said won't work, numbers are combinations of 0123456789, G is not one of those. if you really want easy readable configs then you'll just have to use the char event like I suggested as opposed to the key event, but it means people will be restricted to only being able to assign keys that have character representations (i.e. numbers, letters, symbols. not arrows, or control, etc)
KingofGamesYami #12
Posted 01 July 2014 - 12:40 AM
You could check both events, so people can set things as characters OR keys.

while true do --#since both a key and char event are generated, we need a loop
 local event = {os.pullEvent()}  --#since we have two different events to capture, we use plain os.pullEvent()
 if event[1] == "key" and event[2] == tonumber( confighotkey ) then --#since we need a number to compare, we compare the results of tonumber()
  --#do whatever
 elseif event[1] == "char" and event[2] == confighotkey then  --#since we need a string, we leave it alone.
  --#do whatever
 end
end
hbomb79 #13
Posted 01 July 2014 - 04:39 AM
hmmm, All i want is for them to be able to edit the hotkeys by typing in the letter, not a random number…
theoriginalbit #14
Posted 01 July 2014 - 05:23 AM
is English not your native language, are you genuinely not understanding what I am typing, or are you paying no attention to what I am saying?
hbomb79 #15
Posted 01 July 2014 - 05:26 AM
Well theres no need to be rude to me for not understanding how the code works and how to implement it, If you are going to insult me then just ignore me, Im asking for help, not demanding it… Sorry for annoying you, I guess ill scrap the idea
theoriginalbit #16
Posted 01 July 2014 - 05:30 AM
I'm not being rude, or at least it was not my intention, I am trying to figure out where your problem is with what I'm saying. I've given you a solution twice now, the first time I gave an alternative solution which you rejected, so I stated the option that would work a second time (and the slight drawbacks associated with it) and you're still stuck on the rejected solution. I am simply trying to understand where you're not understanding so that it can be rectified.
hbomb79 #17
Posted 01 July 2014 - 05:34 AM
I just dont understand how to convert the "Character" I enter in the config part of the program into the correct number and then write it to the config… I know you've explained it, i just don't understand, like i said. Ill look over what you've said and try it again
theoriginalbit #18
Posted 01 July 2014 - 05:40 AM
I never explained it like that at all… okay let me try and explain further/again.

currently you're using the key event. As shown on that wiki page when a key is pressed it will provide you with the key code of which key is pressed (a full list of key codes can be found here). However in your config file you're storing the letter G, as you can see from that key codes image when the key G is pressed the key code 34 is returned to you through the key event, and as we know 34 and G are not the same, this is where your problem occurs. This is where the char event comes into play for your specific problem. As stated every time a key is pressed there will be a key event, telling you the key code of which key was pressed, this is so you can know if keys such as control, backspace, arrow keys, etc., have been pressed; however with ComputerCraft when a key that has a graphical representation, for example a letter, a number, a symbol, has been pressed you will also receive a char event, this event will return the graphical representation of the key as opposed to a number.

Example:
If you press the enter key you will get the following
Event: key Param: 28
however if you press the g key you will get
Event: key Param: 34
Event: char Param: g

therefore knowing this, if you want to check if the key that was pressed, matches the key specified in the config file, you must listen for the char event, not the key event as you are currently doing.

EDIT: just incase you're unaware, on line 256 you have the following code

elseif event == "key" then
this is what I mean by you 'listening' for a key event.
Edited on 01 July 2014 - 03:42 AM
hbomb79 #19
Posted 01 July 2014 - 05:42 AM
Oh, Perfect thank you, I will try it out and let you know of any problems i run into