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

Ingame Keyboard

Started by MrabEzreb, 05 July 2014 - 05:08 PM
MrabEzreb #1
Posted 05 July 2014 - 07:08 PM
So, i am building an os, sortof, and i want the keyboard to be ingame. I have a monitor that i look at, then i have a monitor facing up that i am displaying a keyboard on. I want the monitor-keyboard to work just like real one, so how would i do that? All i know is that it would probobly use os.queueEvent.
Thanks!
0099 #2
Posted 05 July 2014 - 11:00 PM
You definitely need to use Parallel API to do that. You launch keyboard program parallel to all others. Also, you need to assign some windows ( from Window API ) to keyboard and to other program. The keyboard program does just one thing: it pulls "mouse" event and if it has valid cordinates, it queues corresponding "key" event. Keep in mind, that your other program will hear both "mouse" and "key" events, so, when it gets a mouse click, it should check whether the coordinates belong to its window, and ignore all that correspond to keyboard.

EDIT: it's kind of useful idea, especially it can help to deal with "escape" and other special keys.
Edited on 05 July 2014 - 09:02 PM
Dog #3
Posted 05 July 2014 - 11:39 PM
@0099 I think you're over complicating it. The Window API isn't at all necessary to address multiple monitors (and is not indicated as a necessity in this case based on MrabEzreb's description).

@MrabEzreb - I don't think you'll need os.queueEvent. But you will be using os.pullEvent to caputure the monitor touches (the key presses on the monitor keyboard). Unless you need to change information on the display while your typing, you probably won't even need the parallel API. You can simply use os.pullEvent to capture the input, then display the character 'typed' on the 'main' monitor (it'll happen so fast it'll appear to have happened instantly and simultaneously).

Here is some *very basic* example code (simple if/then, no tables or fancy syntax). This will not work for you, but should give you an idea of what you'll need to do…

local monA = peripheral.wrap("top")   --# 'monitor'
local monB = peripheral.wrap("front") --# 'keyboard'

local inKey
 
while true do
  local touchEvent, touchMon, touchX, touchY = os.pullEvent("monitor_touch")
  if touchMon == "front" then
    if touchY == 1 then     -- first row of keys
      if touchX == 1 then
        inKey = "Q"
      elseif touchX == 2 then
        inKey == "W"
      end
    elseif touchY == 2 then -- second row of keys
      --# do stuff   
    elseif touchY == 3 then -- third row of keys
      --# do stuff   
    end
    monA.write(inKey)
  end
end

Obviously this doesn't include a row for numbers or for the space bar and 'special' keys, and it doesn't address the issue of cursor placement (using setCursorPos) - again, this is just a basic example to give you an idea of how you can approach this. There are much better ways to do this, but I'm hoping this will show you the basic logic you'll need to accomplish what you want.

Hope that helps :)/>
Edited on 05 July 2014 - 10:10 PM
theoriginalbit #4
Posted 06 July 2014 - 03:37 AM
If you're looking at adding this actually into your program, and not just using it during development (its a little too much work to implement for use in development), I do have an API that will render an Android/iOS style keyboard to any screen, called ccKeyboard (you can find it under the APIs spoiler in my programs thread).
Edited on 06 July 2014 - 01:41 AM