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

Limit read

Started by HurricaneCoder, 11 June 2013 - 10:45 AM
HurricaneCoder #1
Posted 11 June 2013 - 12:45 PM
So I am writing a function that limit how many character can be press. So basically I said event,char = os.pullEvent() if event == "char" then print("ok") elseif event == "key" then print("key") end. I don't understand why everytime it just auto default to key. I know that if I do os.pullEvent("char") it will only pull an event of a character press, however I also need key in my limit read function as well. Is there anyway around this?
M4sh3dP0t4t03 #2
Posted 11 June 2013 - 01:03 PM
I never used char events, but I think it would be better to make it with"os.pullEvent("key")" and a table that includes all key codes.
PixelToast #3
Posted 11 June 2013 - 01:05 PM
the key event is triggered when you press any key, it returns a number 0-255
the char event is there to convert the number into a character
this is intended
HurricaneCoder #4
Posted 11 June 2013 - 01:14 PM
the key event is triggered when you press any key, it returns a number 0-255
the char event is there to convert the number into a character
this is intended
I do realize that char is just a conversion of key however I saw people do os.pullEvent() and they can use both char and key.
Engineer #5
Posted 11 June 2013 - 01:42 PM
I do realize that char is just a conversion of key however I saw people do os.pullEvent() and they can use both char and key.

BUT if the key you are pressing is similair to something like shift, or enter. Then you cannot pull that with the "char" filter. That is the only possible way of tracking that. Also, in CC we have a keys API. You can do this for the most keys:

local e, s = os.pullEvent("key")
if s == keys.enter then
  -- blabla
end
Here is a full list: http://www.computercraft.info/wiki/Keys_(API)

Also, when you want to check for a button, but it is not in the keys API, take a look over here: http://www.computercraft.info/wiki/Key_(event)
HurricaneCoder #6
Posted 11 June 2013 - 02:02 PM
So I was looking up how to limit the character in read and I found this code by Grim Reaper

function limitRead( nLength, cReplaceChar )
    term.setCursorBlink( true )
    nLength = nLength or -1 -- -1 is unlimited
    sReturnString = ""
    xPos, yPos = term.getCursorPos()
    while true do
	    event, char = os.pullEvent()
	    if nLength ~= -1 and string.len( sReturnString ) >= nLength then term.setCursorBlink( false ); return sReturnString end -- Length check
	    if event == "char" then sReturnString = sReturnString .. char
	    elseif event == "key" and char == 28 then term.setCursorBlink( false ); return sReturnString -- Enter
	    elseif event == "key" and char == 14 then -- Backspace
		    term.setCursorPos( xPos, yPos )
		    term.write( string.rep( " ", string.len( sReturnString ) ) )
		    sReturnString = string.sub( sReturnString, 1, string.len( sReturnString )-1 )
		    term.setCursorPos( xPos, yPos )
		    if not cReplaceChar then term.write( sReturnString )
		    else term.write( string.rep( cReplaceChar, string.len( sReturnString ) ) ) end
	    end
	    term.setCursorPos( xPos, yPos )
	    term.write( string.rep( " ", string.len( sReturnString ) ) )
	    term.setCursorPos( xPos, yPos )
	    if not cReplaceChar then term.write( sReturnString )
	    else term.write( string.rep( cReplaceChar, string.len( sReturnString ) ) ) end
    end
end
My question was that his os.pullEvent() respond to char and key. His event can do if event == char then blablabla. When I do an os.pullEvent() in lua for test mine only respond to key so when I said if event == char then blabla however nothing come out because my event is = to key not char. I wonder how Grim did his to respond to both. .
LBPHacker #7
Posted 11 June 2013 - 03:08 PM
He did it by not giving any argument to os.pullEvent at all. That way it will return on every event, both key and char (and a lot of others).
Keep in mind that the char event gets fired only if you press a key that types a printable character, so while K (with no Shift) will appear as "k" in the char event, Control won't appear at all - only in the key event as 29 (left) or 157 (right).
HurricaneCoder #8
Posted 11 June 2013 - 03:26 PM
He did it by not giving any argument to os.pullEvent at all. That way it will return on every event, both key and char (and a lot of others).
Keep in mind that the char event gets fired only if you press a key that types a printable character, so while K (with no Shift) will appear as "k" in the char event, Control won't appear at all - only in the key event as 29 (left) or 157 (right).

I test this and it only return key without argument. Basically just like my question I already use os.pullEvent() without any argument but it still return key.
Lyqyd #9
Posted 11 June 2013 - 04:30 PM
Threads merged. Stick to one topic for questions about a single idea/piece of code, please.
LBPHacker #10
Posted 11 June 2013 - 04:31 PM
What happened here… Lyqyd, was that you? (EDIT: Yup)

Anyways, Hurricane, give us your code then, that's the only way we can help.
HurricaneCoder #11
Posted 11 June 2013 - 04:59 PM
What happened here… Lyqyd, was that you? (EDIT: Yup)

Anyways, Hurricane, give us your code then, that's the only way we can help.

I have not finish and code yet but I go to lua and do an os.pullEvent() but it only seem to give me the key instead of giving me and char at the same time.
Engineer #12
Posted 11 June 2013 - 05:12 PM
That is because you are only pulling one event. The key event gets queued first, and the next pullEvent will trigger the char event. Try this:

while true do print(table.concat({os.pullEvent()}, ', ')) end

Credits for the line go to LBPHacker, he gave me this line when I was starting out :)/>
HurricaneCoder #13
Posted 11 June 2013 - 05:28 PM
That is because you are only pulling one event. The key event gets queued first, and the next pullEvent will trigger the char event. Try this:

while true do print(table.concat({os.pullEvent()}, ', ')) end

Credits for the line go to LBPHacker, he gave me this line when I was starting out :)/>

Alright Finally, thanks LBPHacker and you too Engineer