local en = "qwertyuiop[]asdfghjkl;'zxcvbnm,./`QWERTYUIOP{}ASDFGHJKL:\"ZXCVBNM<>?~"
local ru = "йцукенгшщзхъфывапролджэячсмитьбю.ёЙЦУКЕНГШЩЗХЪФЫВАПРОЛДЖЭЯЧСМИТЬБЮ,Ё"
local layout_map = {}
local enabled = true
local processing = false
function main()
for i = 1, #en do
layout_map[ru:sub(i, i)] = en:sub(i, i)
layout_map[en:sub(i, i)] = ru:sub(i, i)
end
repeat
wait(50)
until isSampAvailable()
sampRegisterChatCommand("kfix", toggleFix)
sampRegisterChatInput(processChatInput)
sampAddChatMessage("{00FF00}[KeyboardFix]: {FFFFFF}Автокоррекция включена. /kfix", -1)
end
function processChatInput(text)
if not enabled or processing then return end
processing = true
local has_ru = false
local has_en = false
for i = 1, #text do
local char = text:sub(i, i)
if char:match("[\192-\255]") or char == "ё" or char == "Ё" then
has_ru = true
elseif char:match("[a-zA-Z]") then
has_en = true
end
end
if has_ru and has_en then
processing = false
return
end
local convert_to_en = has_ru
local convert_to_ru = has_en
local _, cursor_pos = sampGetChatInputInfo()
cursor_pos = cursor_pos + 1
local new_text = ""
local new_cursor_offset = 0
for i = 1, #text do
local char = text:sub(i, i)
local new_char = char
if convert_to_en and layout_map[char] then
new_char = layout_map[char]
elseif convert_to_ru and layout_map[char] then
new_char = layout_map[char]
end
new_text = new_text .. new_char
if i == cursor_pos and new_char ~= char then
new_cursor_offset = #new_text - i
end
end
if new_text ~= text then
sampSetChatInputText(new_text, cursor_pos - 1 + new_cursor_offset)
end
processing = false
end
function toggleFix()
enabled = not enabled
local state = enabled and "{00FF00}включена" or "{FF0000}выключена"
sampAddChatMessage("{00FF00}[KeyboardFix]: {FFFFFF}Автокоррекция "..state, -1)
end
function onScriptTerminate(script, quitGame)
if script == thisScript() then
sampUnregisterChatInput(processChatInput)
end
end