Skip to content

Commit bfa1016

Browse files
committed
Reorganise keyboard library
also incremented version
1 parent aa4949b commit bfa1016

File tree

3 files changed

+71
-47
lines changed

3 files changed

+71
-47
lines changed

miniOS/keyboard.lua

Lines changed: 1 addition & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
local keyboard = {pressedChars = {}, pressedCodes = {}}
1+
local keyboard = {}
22

33
keyboard.keys = {
44
["1"] = 0x02,
@@ -130,44 +130,4 @@ keyboard.keys = {
130130
numpadequals = 0x8D,
131131
}
132132

133-
-- Create inverse mapping for name lookup.
134-
do
135-
local keys = {}
136-
for k in pairs(keyboard.keys) do
137-
table.insert(keys, k)
138-
end
139-
for _, k in pairs(keys) do
140-
keyboard.keys[keyboard.keys[k]] = k
141-
end
142-
end
143-
144-
-------------------------------------------------------------------------------
145-
146-
function keyboard.isAltDown()
147-
return (keyboard.pressedCodes[keyboard.keys.lmenu] or keyboard.pressedCodes[keyboard.keys.rmenu]) ~= nil
148-
end
149-
150-
function keyboard.isControl(char)
151-
return type(char) == "number" and (char < 0x20 or (char >= 0x7F and char <= 0x9F))
152-
end
153-
154-
function keyboard.isControlDown()
155-
return (keyboard.pressedCodes[keyboard.keys.lcontrol] or keyboard.pressedCodes[keyboard.keys.rcontrol]) ~= nil
156-
end
157-
158-
function keyboard.isKeyDown(charOrCode)
159-
checkArg(1, charOrCode, "string", "number")
160-
if type(charOrCode) == "string" then
161-
return keyboard.pressedChars[charOrCode]
162-
elseif type(charOrCode) == "number" then
163-
return keyboard.pressedCodes[charOrCode]
164-
end
165-
end
166-
167-
function keyboard.isShiftDown()
168-
return (keyboard.pressedCodes[keyboard.keys.lshift] or keyboard.pressedCodes[keyboard.keys.rshift]) ~= nil
169-
end
170-
171-
-------------------------------------------------------------------------------
172-
173133
return keyboard

miniOS/minios.lua

Lines changed: 69 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
_G._OSNAME = "miniOS"
2-
_G._OSVER = "0.6.1.4"
2+
_G._OSVER = "0.6.1.5"
33
_G._OSVERSION = _OSNAME .. " " .. _OSVER
4+
_G._OSCREDIT = "miniOS by Skye, based off of OpenOS code from OpenComputers.\nminiOS code is under BSD 2-clause license, OpenOS code is under the MIT liecence."
45

56
--component code
67
function component_code()
@@ -1040,7 +1041,70 @@ function terminal_code()
10401041

10411042
return term
10421043
end
1043-
function keyboard_init()
1044+
1045+
function keyboard_code(keyboard_data)
1046+
local keyboard = {pressedChars = {}, pressedCodes = {}, keys = keyboard_data.keys}
1047+
1048+
-- Create inverse mapping for name lookup.
1049+
setmetatable(keyboard.keys, {
1050+
__index = function(tbl, k)
1051+
if type(k) ~= "number" then return end
1052+
for name,value in pairs(tbl) do
1053+
if value == k then
1054+
return name
1055+
end
1056+
end
1057+
end
1058+
})
1059+
1060+
local function getKeyboardAddress(address)
1061+
return address or term.keyboard.address
1062+
end
1063+
1064+
local function getPressedCodes(address)
1065+
address = getKeyboardAddress(address)
1066+
return address and keyboard.pressedCodes[address] or false
1067+
end
1068+
1069+
local function getPressedChars(address)
1070+
address = getKeyboardAddress(address)
1071+
return address and keyboard.pressedChars[address] or false
1072+
end
1073+
1074+
function keyboard.isAltDown(address)
1075+
checkArg(1, address, "string", "nil")
1076+
local pressedCodes = getPressedCodes(address)
1077+
return pressedCodes and (pressedCodes[keyboard.keys.lmenu] or pressedCodes[keyboard.keys.rmenu]) ~= nil
1078+
end
1079+
1080+
function keyboard.isControl(char)
1081+
return type(char) == "number" and (char < 0x20 or (char >= 0x7F and char <= 0x9F))
1082+
end
1083+
1084+
function keyboard.isControlDown(address)
1085+
checkArg(1, address, "string", "nil")
1086+
local pressedCodes = getPressedCodes(address)
1087+
return pressedCodes and (pressedCodes[keyboard.keys.lcontrol] or pressedCodes[keyboard.keys.rcontrol]) ~= nil
1088+
end
1089+
1090+
function keyboard.isKeyDown(charOrCode, address)
1091+
checkArg(1, charOrCode, "string", "number")
1092+
checkArg(2, address, "string", "nil")
1093+
if type(charOrCode) == "string" then
1094+
local pressedChars = getPressedChars(address)
1095+
return pressedChars and pressedChars[utf8 and utf8.codepoint(charOrCode) or charOrCode:byte()]
1096+
elseif type(charOrCode) == "number" then
1097+
local pressedCodes = getPressedCodes(address)
1098+
return pressedCodes and pressedCodes[charOrCode]
1099+
end
1100+
end
1101+
1102+
function keyboard.isShiftDown(address)
1103+
checkArg(1, address, "string", "nil")
1104+
local pressedCodes = getPressedCodes(address)
1105+
return pressedCodes and (pressedCodes[keyboard.keys.lshift] or pressedCodes[keyboard.keys.rshift]) ~= nil
1106+
end
1107+
10441108
local function onKeyDown(_, address, char, code)
10451109
if keyboard.pressedChars[address] then
10461110
keyboard.pressedChars[address][char] = true
@@ -1198,8 +1262,7 @@ component_code()
11981262
text = text_code()
11991263
filesystem = fs_code()
12001264
fs = filesystem
1201-
keyboard = dofile("keyboard.lua")
1202-
keyboard_init()
1265+
keyboard = keyboard_code(dofile("keyboard.lua"))
12031266
term = terminal_code()
12041267

12051268
-- set up other functions...
@@ -1226,9 +1289,10 @@ if term.isAvailable() then
12261289
end
12271290

12281291
print("Starting " .. _OSNAME .. "...\n")
1292+
print(__OSCREDIT)
12291293

12301294
--clean up libs
1231-
event_code, component_code, text_code, fs_code, terminal_code, keyboard_init = nil, nil, nil, nil, nil, nil
1295+
event_code, component_code, text_code, fs_code, terminal_code, keyboard_code = nil, nil, nil, nil, nil, nil
12321296

12331297
--map the drives
12341298
for address, componentType in component.list() do

miniOS/version.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
miniOS
2-
0.6.1.4
2+
0.6.1.5
33
Alpha

0 commit comments

Comments
 (0)