I'm writing a custom OS for educational purposes and I'm having issues getting keyboard input.
Everything completely works in OpenOS so everything is hooked up correctly.
My init.lua code
do
_G._OSVERSION = "0.1"
_G._OSRELEASE = "ALPHA"
_G._OSNAME = "C_OS"
local rom = {}
function rom.invoke(method, ...)
return component.invoke(computer.getBootAddress(), method, ...)
end
function rom.open(file) return rom.invoke("open", file) end
function rom.read(handle) return rom.invoke("read", handle, math.huge) end
function rom.close(handle) return rom.invoke("close", handle) end
function rom.isDirectory(path) return rom.invoke("isDirectory", path) end
_G.rom = rom
screen = component.list("screen", true)()
for address in component.list("screen", true) do
if #component.invoke(address, "getKeyboards") > 0 then
screen = address
end
end
local gpu = component.list("gpu", true)()
local w, h
if gpu and screen then
component.invoke(gpu, "bind", screen)
w,h = component.invoke(gpu, "maxResolution")
component.invoke(gpu, "setResolution", w, h)
component.invoke(gpu, "setBackground", 0x000000)
component.invoke(gpu, "setForeground", 0xFFFFFF)
component.invoke(gpu, "fill", 1, 1, w, h, " ")
end
function write(msg, x, y)
if gpu and screen then
component.invoke(gpu, "set", x, y, msg)
end
end
local y = 1
function clearscreen()
component.invoke(gpu, "fill", 1, 1, w, h, " ")
y = 1
end
function print(msg)
if y == h then
clearscreen()
y = 1
end
write(msg,1,y)
y = y + 1
end
function loadfile(file)
local handle, reason = rom.open(file)
if not handle then
error(reason)
end
local buffer = ""
repeat
local data, reason = rom.read(handle)
if not data and reason then
error(reason)
end
buffer = buffer .. (data or "")
until not data
rom.close(handle)
return load(buffer, "=" .. file)
end
end
_G.loadfile = loadfile
_G.print = print
_G.write = write
_G.clearscreen = clearscreen
local a = loadfile("/lib/reg.lua")
a().register("/lib/reg.lua")
register("/test.lua")
register("/lib/os.lua")
keyboard = dofile("/lib/keyboard.lua")
_G.keyboard = keyboard
test()
while true do
clearscreen()
print("Keys:")
for a,b in pairs(keyboard.pressedCodes) do
print(a,
end
sleep(1)
end
Here is my keyboard code
local keyboard = {pressedChars = {}, pressedCodes = {}}
keyboard.keys = {
["1"] = 0x02,
["2"] = 0x03,
["3"] = 0x04,
["4"] = 0x05,
["5"] = 0x06,
["6"] = 0x07,
["7"] = 0x08,
["8"] = 0x09,
["9"] = 0x0A,
["0"] = 0x0B,
a = 0x1E,
b = 0x30,
c = 0x2E,
d = 0x20,
e = 0x12,
f = 0x21,
g = 0x22,
h = 0x23,
i = 0x17,
j = 0x24,
k = 0x25,
l = 0x26,
m = 0x32,
n = 0x31,
o = 0x18,
p = 0x19,
q = 0x10,
r = 0x13,
s = 0x1F,
t = 0x14,
u = 0x16,
v = 0x2F,
w = 0x11,
x = 0x2D,
y = 0x15,
z = 0x2C,
apostrophe = 0x28,
at = 0x91,
back = 0x0E, -- backspace
backslash = 0x2B,
colon = 0x92,
comma = 0x33,
enter = 0x1C,
equals = 0x0D,
grave = 0x29, -- accent grave
lbracket = 0x1A,
lcontrol = 0x1D,
lmenu = 0x38, -- left Alt
lshift = 0x2A,
minus = 0x0C,
numlock = 0x45,
pause = 0xC5,
period = 0x34,
rbracket = 0x1B,
rcontrol = 0x9D,
rmenu = 0xB8, -- right Alt
rshift = 0x36,
scroll = 0x46, -- Scroll Lock
semicolon = 0x27,
slash = 0x35, -- / on main keyboard
space = 0x39,
stop = 0x95,
tab = 0x0F,
underline = 0x93,
-- Keypad (and numpad with numlock off)
up = 0xC8,
down = 0xD0,
left = 0xCB,
right = 0xCD,
home = 0xC7,
["end"] = 0xCF,
pageUp = 0xC9,
pageDown = 0xD1,
insert = 0xD2,
delete = 0xD3,
-- Function keys
f1 = 0x3B,
f2 = 0x3C,
f3 = 0x3D,
f4 = 0x3E,
f5 = 0x3F,
f6 = 0x40,
f7 = 0x41,
f8 = 0x42,
f9 = 0x43,
f10 = 0x44,
f11 = 0x57,
f12 = 0x58,
f13 = 0x64,
f14 = 0x65,
f15 = 0x66,
f16 = 0x67,
f17 = 0x68,
f18 = 0x69,
f19 = 0x71,
-- Japanese keyboards
kana = 0x70,
kanji = 0x94,
convert = 0x79,
noconvert = 0x7B,
yen = 0x7D,
circumflex = 0x90,
ax = 0x96,
-- Numpad
numpad0 = 0x52,
numpad1 = 0x4F,
numpad2 = 0x50,
numpad3 = 0x51,
numpad4 = 0x4B,
numpad5 = 0x4C,
numpad6 = 0x4D,
numpad7 = 0x47,
numpad8 = 0x48,
numpad9 = 0x49,
numpadmul = 0x37,
numpaddiv = 0xB5,
numpadsub = 0x4A,
numpadadd = 0x4E,
numpaddecimal = 0x53,
numpadcomma = 0xB3,
numpadenter = 0x9C,
numpadequals = 0x8D,
}
-- Create inverse mapping for name lookup.
do
local keys = {}
for k in pairs(keyboard.keys) do
table.insert(keys, k)
end
for _, k in pairs(keys) do
keyboard.keys[keyboard.keys[k]] = k
end
end
-------------------------------------------------------------------------------
function keyboard.isAltDown()
return (keyboard.pressedCodes[keyboard.keys.lmenu] or keyboard.pressedCodes[keyboard.keys.rmenu]) ~= nil
end
function keyboard.isControl(char)
return type(char) == "number" and (char < 0x20 or (char >= 0x7F and char <= 0x9F))
end
function keyboard.isControlDown()
return (keyboard.pressedCodes[keyboard.keys.lcontrol] or keyboard.pressedCodes[keyboard.keys.rcontrol]) ~= nil
end
function keyboard.isKeyDown(charOrCode)
checkArg(1, charOrCode, "string", "number")
if type(charOrCode) == "string" then
return keyboard.pressedChars[charOrCode]
elseif type(charOrCode) == "number" then
return keyboard.pressedCodes[charOrCode]
end
end
function keyboard.isShiftDown()
return (keyboard.pressedCodes[keyboard.keys.lshift] or keyboard.pressedCodes[keyboard.keys.rshift]) ~= nil
end
-------------------------------------------------------------------------------
return keyboard
I've copied the dofile function from OpenOS so it should work with my code.
You can post now and register later.
If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.
I'm writing a custom OS for educational purposes and I'm having issues getting keyboard input.
Everything completely works in OpenOS so everything is hooked up correctly.
My init.lua code
do _G._OSVERSION = "0.1" _G._OSRELEASE = "ALPHA" _G._OSNAME = "C_OS" local rom = {} function rom.invoke(method, ...) return component.invoke(computer.getBootAddress(), method, ...) end function rom.open(file) return rom.invoke("open", file) end function rom.read(handle) return rom.invoke("read", handle, math.huge) end function rom.close(handle) return rom.invoke("close", handle) end function rom.isDirectory(path) return rom.invoke("isDirectory", path) end _G.rom = rom screen = component.list("screen", true)() for address in component.list("screen", true) do if #component.invoke(address, "getKeyboards") > 0 then screen = address end end local gpu = component.list("gpu", true)() local w, h if gpu and screen then component.invoke(gpu, "bind", screen) w,h = component.invoke(gpu, "maxResolution") component.invoke(gpu, "setResolution", w, h) component.invoke(gpu, "setBackground", 0x000000) component.invoke(gpu, "setForeground", 0xFFFFFF) component.invoke(gpu, "fill", 1, 1, w, h, " ") end function write(msg, x, y) if gpu and screen then component.invoke(gpu, "set", x, y, msg) end end local y = 1 function clearscreen() component.invoke(gpu, "fill", 1, 1, w, h, " ") y = 1 end function print(msg) if y == h then clearscreen() y = 1 end write(msg,1,y) y = y + 1 end function loadfile(file) local handle, reason = rom.open(file) if not handle then error(reason) end local buffer = "" repeat local data, reason = rom.read(handle) if not data and reason then error(reason) end buffer = buffer .. (data or "") until not data rom.close(handle) return load(buffer, "=" .. file) end end _G.loadfile = loadfile _G.print = print _G.write = write _G.clearscreen = clearscreen local a = loadfile("/lib/reg.lua") a().register("/lib/reg.lua") register("/test.lua") register("/lib/os.lua") keyboard = dofile("/lib/keyboard.lua") _G.keyboard = keyboard test() while true do clearscreen() print("Keys:") for a,b in pairs(keyboard.pressedCodes) do print(a, end sleep(1) endHere is my keyboard code
local keyboard = {pressedChars = {}, pressedCodes = {}} keyboard.keys = { ["1"] = 0x02, ["2"] = 0x03, ["3"] = 0x04, ["4"] = 0x05, ["5"] = 0x06, ["6"] = 0x07, ["7"] = 0x08, ["8"] = 0x09, ["9"] = 0x0A, ["0"] = 0x0B, a = 0x1E, b = 0x30, c = 0x2E, d = 0x20, e = 0x12, f = 0x21, g = 0x22, h = 0x23, i = 0x17, j = 0x24, k = 0x25, l = 0x26, m = 0x32, n = 0x31, o = 0x18, p = 0x19, q = 0x10, r = 0x13, s = 0x1F, t = 0x14, u = 0x16, v = 0x2F, w = 0x11, x = 0x2D, y = 0x15, z = 0x2C, apostrophe = 0x28, at = 0x91, back = 0x0E, -- backspace backslash = 0x2B, colon = 0x92, comma = 0x33, enter = 0x1C, equals = 0x0D, grave = 0x29, -- accent grave lbracket = 0x1A, lcontrol = 0x1D, lmenu = 0x38, -- left Alt lshift = 0x2A, minus = 0x0C, numlock = 0x45, pause = 0xC5, period = 0x34, rbracket = 0x1B, rcontrol = 0x9D, rmenu = 0xB8, -- right Alt rshift = 0x36, scroll = 0x46, -- Scroll Lock semicolon = 0x27, slash = 0x35, -- / on main keyboard space = 0x39, stop = 0x95, tab = 0x0F, underline = 0x93, -- Keypad (and numpad with numlock off) up = 0xC8, down = 0xD0, left = 0xCB, right = 0xCD, home = 0xC7, ["end"] = 0xCF, pageUp = 0xC9, pageDown = 0xD1, insert = 0xD2, delete = 0xD3, -- Function keys f1 = 0x3B, f2 = 0x3C, f3 = 0x3D, f4 = 0x3E, f5 = 0x3F, f6 = 0x40, f7 = 0x41, f8 = 0x42, f9 = 0x43, f10 = 0x44, f11 = 0x57, f12 = 0x58, f13 = 0x64, f14 = 0x65, f15 = 0x66, f16 = 0x67, f17 = 0x68, f18 = 0x69, f19 = 0x71, -- Japanese keyboards kana = 0x70, kanji = 0x94, convert = 0x79, noconvert = 0x7B, yen = 0x7D, circumflex = 0x90, ax = 0x96, -- Numpad numpad0 = 0x52, numpad1 = 0x4F, numpad2 = 0x50, numpad3 = 0x51, numpad4 = 0x4B, numpad5 = 0x4C, numpad6 = 0x4D, numpad7 = 0x47, numpad8 = 0x48, numpad9 = 0x49, numpadmul = 0x37, numpaddiv = 0xB5, numpadsub = 0x4A, numpadadd = 0x4E, numpaddecimal = 0x53, numpadcomma = 0xB3, numpadenter = 0x9C, numpadequals = 0x8D, } -- Create inverse mapping for name lookup. do local keys = {} for k in pairs(keyboard.keys) do table.insert(keys, k) end for _, k in pairs(keys) do keyboard.keys[keyboard.keys[k]] = k end end ------------------------------------------------------------------------------- function keyboard.isAltDown() return (keyboard.pressedCodes[keyboard.keys.lmenu] or keyboard.pressedCodes[keyboard.keys.rmenu]) ~= nil end function keyboard.isControl(char) return type(char) == "number" and (char < 0x20 or (char >= 0x7F and char <= 0x9F)) end function keyboard.isControlDown() return (keyboard.pressedCodes[keyboard.keys.lcontrol] or keyboard.pressedCodes[keyboard.keys.rcontrol]) ~= nil end function keyboard.isKeyDown(charOrCode) checkArg(1, charOrCode, "string", "number") if type(charOrCode) == "string" then return keyboard.pressedChars[charOrCode] elseif type(charOrCode) == "number" then return keyboard.pressedCodes[charOrCode] end end function keyboard.isShiftDown() return (keyboard.pressedCodes[keyboard.keys.lshift] or keyboard.pressedCodes[keyboard.keys.rshift]) ~= nil end ------------------------------------------------------------------------------- return keyboardI've copied the dofile function from OpenOS so it should work with my code.
Link to post
Share on other sites