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

converting keycode to char

Started by Exerro, 20 January 2013 - 12:46 AM
Exerro #1
Posted 20 January 2013 - 01:46 AM
Im making a program that needs to listen to multiple events so os.pullEvent("char") wont work…and i need to be able to turn keycodes into chars because for some reason the "char" event is never fired when i press a key
I have also tried string.char(p1) but i guess the mc keycodes are different to lua ones because it never returns the right value
Please help!
theoriginalbit #2
Posted 20 January 2013 - 01:51 AM
Im making a program that needs to listen to multiple events so os.pullEvent("char") wont work…and i need to be able to turn keycodes into chars because for some reason the "char" event is never fired when i press a key
I have also tried string.char(p1) but i guess the mc keycodes are different to lua ones because it never returns the right value
Please help!
Yes the ASCII char set has different numbers to the keyboard key bindings of LWJGL… You should be able to use keys.getName( keycode ) … However this will also return things like "CTRL" for other keys just not char… There must be some reason why the char event isn't working, can you show us the relevant code please?
Exerro #3
Posted 20 January 2013 - 02:06 AM
its about 1000 lines spread across 5 programs…so i cant really show code…but i use a program that sleeps between os.pullEvent() so if char and key is fired next to eachother it might not pick up the char
edit: thanks for the help…keys.getName(p1) works :D/>
theoriginalbit #4
Posted 20 January 2013 - 02:14 AM
can't even show the main loop where you are trying to read the char/key?

ahhh your using sleep… thats eats events… meaning your program will sometimes miss some, you should avoid sleeps when using an event loop and instead use timers ( which is how sleep works anyways )…
Exerro #5
Posted 20 January 2013 - 02:18 AM
oh yeah re-read my code it starts a timer and does os.pullEvent() but if a key is repeatedly pressed it would bypass this so it sleeps if it wasnt the timer that triggered the event…
and the input code:
Spoiler

function sky.input(event,p1,p2,p3,p4,p5)
if event == "key" and p1 == 14 then
  sky.event.queue("fquit","done")
elseif (event == "mouse_click" or event == "mouse_drag") and display == "main" then
  data[p3][p2][1] = p2
  data[p3][p2][2] = p3
  data[p3][p2][3] = bcolsel
  data[p3][p2][4] = tcolsel
  data[p3][p2][5] = itemsel
  xPos = p2
  yPos = p3
elseif event == "mouse_click" and display == "menu" then
  if p1 == 1 and (p3 == 3 or p3 == 4) then
   if math.isEven(p2) then
	bcolsel = 2^(((p2-12)/2)-1)
   else
	bcolsel = 2^(((p2-11)/2)-1)
   end
  elseif p1 == 1 and (p3 == 7 or p3 == 8) then
   if math.isEven(p2) then
	tcolsel = 2^(((p2-12)/2)-1)
   else
	tcolsel = 2^(((p2-11)/2)-1)
   end
  elseif p1 == 1 and p2 == 13 and p3 == 10 then
   _, key = os.pullEvent("char")
   itemsel = key
  end
elseif event == "key" and p1 == 29 then
  save()
elseif event == "key" and p1 == 15 and display == "main" then
  display = "menu"
elseif event == "key" and p1 == 15 and display == "menu" then
  display = "main"
elseif event == "key" and p1 == 28 then
  yPos = yPos+1
elseif event == "key" and p1 == 42 then
  nextCaps = true
elseif event == "char" or event == "key" then
  if event == "key" then
   --[[for i = 1,#Key do
	if p1 == Key[i][2] then
	 sleep(10000)
	end
   end]]
   p1 = keys.getName(p1)
  end
  if nextCaps then
   p1 = string.upper(p1)
   nextCaps = false
  end
  data[yPos][xPos][1] = xPos
  data[yPos][xPos][2] = yPos
  data[yPos][xPos][3] = bcolsel
  data[yPos][xPos][4] = tcolsel
  data[yPos][xPos][5] = p1
  xPos = xPos+1
end
end