Jump to content




converting keycodes


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

#1 Waitdev_

  • Members
  • 432 posts
  • LocationAdelaide

Posted 18 May 2015 - 10:48 AM

i'm making a program, and i need it to get a user input. i don't want it to be io.read(), since that makes a new line and i need it to be only 1 letter. if i could get it to use os.pullEvent("key") and convert that to a number/letter, that would make things a lot easier. by the way, i can't do os.pullEvent("char") since i need it to do all characters.

#2 MKlegoman357

  • Members
  • 1,170 posts
  • LocationKaunas, Lithuania

Posted 18 May 2015 - 11:08 AM

There's no reliable solution to using "key" event to get the pressed letter, number or other character. You'll have to use the "char" event. But don't worry, to listen for more than one event at a time just don't pass an event filter. A common event-catching structure in CC is this (an event loop):

while true do
  local e = { os.pullEvent() } --# catch any event and put all it's parameters into a table

  if e[1] == "key" then
    if e[2] == keys.enter then

    elseif ...
  elseif e[1] == "char" then
    local pressedChar = e[2]

    ...
  elseif ...
end


#3 Waitdev_

  • Members
  • 432 posts
  • LocationAdelaide

Posted 18 May 2015 - 11:30 AM

View PostMKlegoman357, on 18 May 2015 - 11:08 AM, said:

There's no reliable solution to using "key" event to get the pressed letter, number or other character. You'll have to use the "char" event. But don't worry, to listen for more than one event at a time just don't pass an event filter. A common event-catching structure in CC is this (an event loop):

while true do
  local e = { os.pullEvent() } --# catch any event and put all it's parameters into a table

  if e[1] == "key" then
	if e[2] == keys.enter then

	elseif ...
  elseif e[1] == "char" then
	local pressedChar = e[2]

	...
  elseif ...
end
its not really detecting the key, its mainly just getting the key you press then printing that key. sorry i didn't really make it too clear :/

#4 Dahknee

  • Members
  • 1,808 posts
  • Location/home/da

Posted 18 May 2015 - 12:40 PM

View PostMKlegoman357, on 18 May 2015 - 11:08 AM, said:

There's no reliable solution to using "key" event to get the pressed letter, number or other character. You'll have to use the "char" event. But don't worry, to listen for more than one event at a time just don't pass an event filter. A common event-catching structure in CC is this (an event loop):

while true do
  local e = { os.pullEvent() } --# catch any event and put all it's parameters into a table

  if e[1] == "key" then
    if e[2] == keys.enter then

    elseif ...
  elseif e[1] == "char" then
    local pressedChar = e[2]

    ...
  elseif ...
end

yes there is? the keys api? keys.getName(number code)
http://computercraft.../Keys_%28API%29

It is useful if you wish to convert it from key to character.

#5 MKlegoman357

  • Members
  • 1,170 posts
  • LocationKaunas, Lithuania

Posted 18 May 2015 - 12:51 PM

What if caps-lock or shift key is on? How do you know if the character should be uppercase or lowercase. Also, the keys table doesn't really have the printable characters, it's their names actually. For example there's 'numPad1' and 'one' instead of the printable character '1'.

To OP: so you don't need a printable representation of the key but rather it's name, like instead of ',' you'd get 'comma'? Then you can use the keys table. If not then please elaborate more on what you are trying to achieve so we could help as best as we can.

#6 Waitdev_

  • Members
  • 432 posts
  • LocationAdelaide

Posted 18 May 2015 - 11:06 PM

i think i'm gonna have to go with converting them all in an array -_-

#7 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 18 May 2015 - 11:48 PM

View PostMKlegoman357, on 18 May 2015 - 11:08 AM, said:

while true do
  local e = { os.pullEvent() } --# catch any event and put all it's parameters into a table

  if e[1] == "key" then
	if e[2] == keys.enter then

	elseif ...
  elseif e[1] == "char" then
	local pressedChar = e[2]

	...
  elseif ...
end

Why would this not work?

#8 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 19 May 2015 - 03:30 AM

View PostWait_, on 18 May 2015 - 10:48 AM, said:

i can't do os.pullEvent("char") since i need it to do all characters.

os.pullEvent("char") does return any pressed character.

Let's assume you mean "all keys". Given that pressing a character key generates a key event followed by a char event, pressing a non-character key only generates a key event, and that you only want to get the first button pressed, I'd do something like this:

local myEvent = {os.pullEvent("key")}

os.queueEvent("dummyEvent")
local myEvent2 = {os.pullEvent()}

if myEvent2[1] == "char" then
    print("Pressed character \""..myEvent2[2].."\".")
else
    print("Pressed key \""..keys.getName(myEvent[2]).."\".")
end






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users