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

Events, Key & Char, can I have them at the same time?

Started by augustas656, 29 April 2014 - 07:24 PM
augustas656 #1
Posted 29 April 2014 - 09:24 PM
If you do something like event,p1,p2,p3 = os.pullEvent(), pressing a will not be char but instead it will be a key event. How can I use the pullevent to get string chars (I don't want to list what character each keyboard numeral relates to.) But I also want to use key, my plan is being able to use char and then make my own read function (that limits how much can be written), you have to click with your mouse to stop writing, and when you press ENTER whilst writing instead of exiting the input you add a "\n" to it which means next line.

How can I do this, do I have to do the numericals and use just key or?
CometWolf #2
Posted 29 April 2014 - 09:30 PM
Just use if statements

while true do
local event,p1,p2,p3 = os.pullEvent()
  if event == "char" then
   
  elseif event == "key" then
   
  end
end
if the keyboard button has a corresponding char, the char event is fired aswell as the key, otherwise only the key is fired.
OczkoSX #3
Posted 29 April 2014 - 09:31 PM
I didn't understand you well, but i think you can make in loop something like this:

while true do
char = os.pullEvent("char")
if char == '/n' then
-- ENTER
else
-- ADD CHAR TO STRING WHAT INCLUDE YOUR TEXT
end

Up was first…
I don't know if you can compare char with enter sign. CometWolf's way is better then my D:

All events types you can check here: http://computercraft.info/wiki/Os.pullEvent
Edited on 29 April 2014 - 07:34 PM
KingofGamesYami #4
Posted 30 April 2014 - 12:45 AM
I actually use something like this in a program I am working on:

event = {os.pullEvent()}
if event[1] == "char" then
 s = s..event[2] --#this will change the variable s to s + the key pressed (basically, this acts like read() without showing what they are typing)
 term.write(event[2]) --#shows what they typed on screen
elseif event[1] == "key" and (event[2] == 14 or event[2] == 211) then --#checks for delete AND backspace
 s = string.sub(s, 1, #s - 1) --#deletes last character.
 local x, y = term.getCursorPos() --#these four lines remove the character from the screen
 term.setCursorPos(x - 1, y)
 term.write(' ')
 term.setCursorPos(x - 1, y)
end
Of course, you may want to loop this, I actually have a third statement that ends the loop and returns s when enter is pressed.
Btw, does anyone have the key codes for a mac keyboard? The one on the wiki shows the standard windows keyboard.
Edited on 29 April 2014 - 11:24 PM
MKlegoman357 #5
Posted 30 April 2014 - 09:07 AM
Btw, does anyone have the key codes for a mac keyboard? The one on the wiki shows the standard windows keyboard.

Keycodes for Mac should be the same as for Windows. If you're using an emulator then it's that emulator's problem.
theoriginalbit #6
Posted 30 April 2014 - 10:21 AM

if char == '/n' then
firstly, newline characters are \n not /n
secondly, pressing enter does not queue a char event, it only queues a key event with the keys.enter key code

local event = { os.pullEvent() }
if event[1] == "char" then
  --# it was a letter, number, or punctuation
elseif event[1] == "key" then
  --# it was any key on the keyboard except tilde (~)
  if event[2] == keys.enter then
	--# enter was pressed
  end
end

Btw, does anyone have the key codes for a mac keyboard? The one on the wiki shows the standard windows keyboard.
Keycodes for Mac should be the same as for Windows. If you're using an emulator then it's that emulator's problem.
They are indeed the same.
Edited on 30 April 2014 - 08:22 AM
OczkoSX #7
Posted 30 April 2014 - 05:19 PM

if char == '/n' then
firstly, newline characters are \n not /n
secondly, pressing enter does not queue a char event, it only queues a key event with the keys.enter key code


I said im not sure ;)/> And yea, i meant \n ;)/>