Jump to content




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


  • You cannot reply to this topic
16 replies to this topic

#1 roosterhat

  • Members
  • 25 posts

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

#2 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 19 February 2013 - 03:41 AM

Split into new topic.

#3 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

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


#4 roosterhat

  • Members
  • 25 posts

Posted 19 February 2013 - 04:58 PM

it prints both though

#5 roosterhat

  • Members
  • 25 posts

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

#6 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 19 February 2013 - 05:00 PM

View Postroosterhat, on 19 February 2013 - 04:58 PM, said:

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 by TheOriginalBIT, 19 February 2013 - 05:03 PM.


#7 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 19 February 2013 - 05:02 PM

- oops thought i hit edit -

Edited by TheOriginalBIT, 19 February 2013 - 05:03 PM.


#8 roosterhat

  • Members
  • 25 posts

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

View Postroosterhat, on 19 February 2013 - 05:06 PM, said:

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

#9 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 19 February 2013 - 05:08 PM

View Postroosterhat, on 19 February 2013 - 05:07 PM, said:

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.

#10 roosterhat

  • Members
  • 25 posts

Posted 19 February 2013 - 05:14 PM

ok thanks that works

#11 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 19 February 2013 - 05:15 PM

no problems

#12 roosterhat

  • Members
  • 25 posts

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

#13 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 19 February 2013 - 05:17 PM

View Postroosterhat, on 19 February 2013 - 05:15 PM, said:

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 by TheOriginalBIT, 19 February 2013 - 05:21 PM.


#14 roosterhat

  • Members
  • 25 posts

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

#15 roosterhat

  • Members
  • 25 posts

Posted 19 February 2013 - 05:30 PM

oh and the the backspace is not working for me

#16 roosterhat

  • Members
  • 25 posts

Posted 19 February 2013 - 05:35 PM

oh never mind i fixed it
missing the #

#17 roosterhat

  • Members
  • 25 posts

Posted 19 February 2013 - 05:37 PM

thanks so much now i can move on to more important things in my program :)





3 user(s) are reading this topic

0 members, 3 guests, 0 anonymous users