<commit_before>AddCSLuaFile("cl_init.lua")
AddCSLuaFile("shared.lua")
AddCSLuaFile("remap.lua")

include('shared.lua')
include('remap.lua')

ENT.WireDebugName = "Wired Keyboard"

local All_Enums = {} -- table containing key -> key enum conversion

-- Add a few common keys
for i = 48, 57 do -- 0 -> 9
	All_Enums[i] = _G["KEY_" .. string.char(i)]
end
for i = 65, 90 do -- A -> Z
	All_Enums[i] = _G["KEY_" .. string.upper(string.char(i))]
end
for i = 97, 122 do -- a -> z
	All_Enums[i] = _G["KEY_" .. string.upper(string.char(i))]
end

local unprintable_chars = {}
for i=17,20 do unprintable_chars[i] = true end -- arrow keys
for i=127,177 do unprintable_chars[i] = true end -- backspace, numpad, ctrl, alt, shift, break, F1-F12, scroll/num/caps lock, and more

local convertable_chars = {
	[128] = 49, -- numpad 1
	[129] = 50, -- numpad 2
	[130] = 51, -- numpad 3
	[131] = 52, -- numpad 4
	[132] = 53, -- numpad 5
	[133] = 54, -- numpad 6
	[134] = 55, -- numpad 7
	[135] = 56, -- numpad 8
	[136] = 57, -- numpad 9
	[137] = 58, -- numpad 4
	[138] = 47, -- /
	[139] = 42, -- *
	[140] = 45, -- -
	[141] = 43, -- +
	[142] = 10, -- \n
	[143] = 46, -- .
}

local function getPrintableChar( key )
	if key == 0 then return "" end
	if unprintable_chars[key] and not convertable_chars[key] then return "" end
	if convertable_chars[key] then key = convertable_chars[key] end
	if key == 13 then key = 10 end -- convert newline '13' into newlne '10' to make it work properly
	return utf8.char(key)
end

function ENT:Initialize()
	self:PhysicsInit(SOLID_VPHYSICS)
	self:SetMoveType(MOVETYPE_VPHYSICS)
	self:SetSolid(SOLID_VPHYSICS)
	self:SetUseType(SIMPLE_USE)

	self.Inputs = WireLib.CreateInputs(self, { "Kick", "Reset Output String" })
	self.Outputs = WireLib.CreateOutputs(self, { "Memory", "Output [STRING]", "OutputChar [STRING]", "ActiveKeys [ARRAY]", "User [ENTITY]", "InUse" })

	self.ActiveKeys = {} -- table containing all currently active keys, used to see when keys are pressed/released
	self.Buffer = {} -- array containing all currently active keys, value is ascii<commit_msg>Fix numpad keys being mapped incorrectly<commit_after>AddCSLuaFile("cl_init.lua")
AddCSLuaFile("shared.lua")
AddCSLuaFile("remap.lua")

include('shared.lua')
include('remap.lua')

ENT.WireDebugName = "Wired Keyboard"

local All_Enums = {} -- table containing key -> key enum conversion

-- Add a few common keys
for i = 48, 57 do -- 0 -> 9
	All_Enums[i] = _G["KEY_" .. string.char(i)]
end
for i = 65, 90 do -- A -> Z
	All_Enums[i] = _G["KEY_" .. string.upper(string.char(i))]
end
for i = 97, 122 do -- a -> z
	All_Enums[i] = _G["KEY_" .. string.upper(string.char(i))]
end

local unprintable_chars = {}
unprintable_chars[0] = true
for i=17,20 do unprintable_chars[i] = true end -- arrow keys
for i=127,177 do unprintable_chars[i] = true end -- backspace, numpad, ctrl, alt, shift, break, F1-F12, scroll/num/caps lock, and more

-- These keys output a numeric code that's different to their ASCII code - they
-- are numpad keys, and using a different code lets contraptions differentiate
-- between eg. pressing 0 on the number row and 0 on the numpad. The codes are
-- defined in remap.lua.
local convertable_chars = {
	[128] = "0",
	[129] = "1",
	[130] = "2",
	[131] = "3",
	[132] = "4",
	[133] = "5",
	[134] = "6",
	[135] = "7",
	[136] = "8",
	[137] = "9",
	[138] = "/",
	[139] = "*",
	[140] = "-",
	[141] = "+",
	[142] = "\n",
	[143] = ".",
}

local function getPrintableChar( key )
	if convertable_chars[key] then return convertable_chars[key] end
	if unprintable_chars[key] then return "" end
	if key == 13 then key = 10 end -- convert newline '13' into newlne '10' to make it work properly
	return utf8.char(key)
end

function ENT:Initialize()
	self:PhysicsInit(SOLID_VPHYSICS)
	self:SetMoveType(MOVETYPE_VPHYSICS)
	self:SetSolid(SOLID_VPHYSICS)
	self:SetUseType(SIMPLE_USE)

	self.Inputs = WireLib.CreateInputs(self, { "Kick", "Reset Output String" })
	self.Outputs = WireLib.CreateOutputs(self, { "Memory", "Output [STRING]", "OutputChar [STRING]", "ActiveKeys [ARRAY]", "User [ENTITY]", "InUse" })

	self.ActiveKeys = {} -- table containing all currently active keys, used to see when keys are pressed/released
	self.Buffer = {} -- array containing all currently active keys, value is ascii
{'fraction_non_alphanumeric': 0.16120729995320543, 'fraction_numerical': 0.052877866167524566, 'mean_word_length': 3.8969072164948453, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2126074', 'n_tokens_mistral': 1798, 'n_tokens_neox': 1590, 'n_words': 472}