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

How do i capture both key and a char event in the os.pullevent

Started by roosterhat, 18 February 2013 - 03:23 PM
roosterhat #1
Posted 18 February 2013 - 04:23 PM
i want it to capture the char mainly but i also want to capture the enter and backspace keys too
Lyqyd #2
Posted 19 February 2013 - 03:41 AM
Split into new topic.
theoriginalbit #3
Posted 19 February 2013 - 03:51 AM
It goes a little something like this:

while true do
  local event, param1 = os.pullEvent()
  if event == "key" then
    -- key was pressed
  elseif event == "char" then
    -- key with a char was pressed
  end
end
roosterhat #4
Posted 19 February 2013 - 04:58 PM
it prints both though
roosterhat #5
Posted 19 February 2013 - 04:59 PM
im trying to make a text box thats has edges and i need to use char over io.read() and i need to be able to capture enter and delete
theoriginalbit #6
Posted 19 February 2013 - 05:00 PM
it prints both though
Not if you do it write. Post the code you have done. :)/>


function read() do
  local input = ""
  while true do
	local event, p = os.pullEvent()
	if event == "char" then
	  input = input..p
	elseif event == "key" then
	  if p == keys.enter then
		return input
	  elseif p == keys.backspace then
		input = input:sub(1, #input - 1)
	  end
	end
  end
  error( "should never get here" )
end
Edited on 19 February 2013 - 04:03 PM
theoriginalbit #7
Posted 19 February 2013 - 05:02 PM
- oops thought i hit edit -
Edited on 19 February 2013 - 04:03 PM
roosterhat #8
Posted 19 February 2013 - 05:07 PM
i could do

keys = {}
while true do
  keys[1]=""
  keys[2]=""
  local event, param1 = os.pullEvent()
  if event == "key" then
    key[1] = param1
  elseif event == "char" then
    key[2] = param2
  end
end

i could do

keys = {}
while true do
  keys[1]=""
  keys[2]=""
  local event, param1 = os.pullEvent()
  if event == "key" then
	key[1] = param1
  elseif event == "char" then
	key[2] = param2
  end
end
no that prints the key ID then two spaces followed by the Char
theoriginalbit #9
Posted 19 February 2013 - 05:08 PM
no that prints the key ID then two spaces followed by the Char
I don't even see how it does that. you have no print in there. Id suggest using a modification of the code I posted above.
roosterhat #10
Posted 19 February 2013 - 05:14 PM
ok thanks that works
theoriginalbit #11
Posted 19 February 2013 - 05:15 PM
no problems
roosterhat #12
Posted 19 February 2013 - 05:15 PM
ill just have to modify your coded so that it prints out the key every time instead of after the enter
theoriginalbit #13
Posted 19 February 2013 - 05:17 PM
ill just have to modify your coded so that it prints out the key every time instead of after the enter
For that you would do



function read() do
  local input = ""
  local cx,cy = term.getCursorPos()
  while true do
	local event, p = os.pullEvent()
	if event == "char" then
	  input = input..p
	elseif event == "key" then
	  if p == keys.enter then
		return input
	  elseif p == keys.backspace then
		input = input:sub(1, #input - 1)
	  end
	end
	term.setCursorPos(cx,cy)
	write(input.." ")
  end
  error( "should never get here" )
end
then you use it just the same as normal read.

write("Your name: ")
local name = read()

could even add a password mask just like the real read. would call it like this:

write("Your pass: ")
local pass = read("*")
and in the read code you would do this

function read( mask )
  -- .... other code here

  term.setCursorPos(cx,cy)
  write( ( mask and string.rep(mask, #input) or input ).." ")
end
Edited on 19 February 2013 - 04:21 PM
roosterhat #14
Posted 19 February 2013 - 05:29 PM
i debated using the read but i want the text to stop at the end of a text box for the login and for the chat box and i dont know of a way to abort read with the enter key
roosterhat #15
Posted 19 February 2013 - 05:30 PM
oh and the the backspace is not working for me
roosterhat #16
Posted 19 February 2013 - 05:35 PM
oh never mind i fixed it
missing the #
roosterhat #17
Posted 19 February 2013 - 05:37 PM
thanks so much now i can move on to more important things in my program :)/>