подмена ввода баг

Okak_Pon

Активный
Автор темы
370
75
Версия MoonLoader
Другое
Почему когда скрипт активен и я ввожу что-то в чат, то в чат пишется сначала то что я нажал, а затем то на что надо было подменить? Вроде блокирую через
consumeWindowMessage


Lua:
local chatReplaceVar = -1
local inputIndex = 1
local replacingString = ""
local lastKeyTime = 0
local keyDelay = 100

function main()
    while not isSampAvailable() do wait(100) end
    
    sampRegisterChatCommand('replaceon', function(param)
        if param and param ~= "" then
            chatReplaceVar = tonumber(param)
            printChat(('Подмена ввода включена: '..tostring(chatReplaceVar)))
        else
            chatReplaceVar = 123456
            printChat(('Подмена ввода включена: 123456'))
        end
        replacingString = tostring(chatReplaceVar)
        inputIndex = 1
    end)
    
    sampRegisterChatCommand('replaceoff', function()
        chatReplaceVar = -1
        inputIndex = 1
        printChat(('Подмена ввода отключена'))
    end)
    
    printChat(('Скрипт загружен. Команды: /replaceon [число] /replaceoff'))
    
    while true do
        wait(0)
        if not sampIsChatInputActive() then
            inputIndex = 1
        end
    end
end

addEventHandler('onWindowMessage', function(msg, wparam, lparam)
    if chatReplaceVar == -1 then return end
    if not sampIsChatInputActive() then return end
    
    replacingString = tostring(chatReplaceVar)
    
    if msg == 0x0100 then
        local currentTime = os.clock() * 1000
        
        if wparam == 8 then
            if inputIndex > 1 then
                inputIndex = inputIndex - 1
            end
            return true
        end
        
        if wparam == 13 then
            inputIndex = 1
            return false
        end

        if inputIndex <= #replacingString then
            local nextDigit = tonumber(string.sub(replacingString, inputIndex, inputIndex))
            
            if currentTime - lastKeyTime > keyDelay then
                keyStroke(nextDigit, 100)
                inputIndex = inputIndex + 1
                lastKeyTime = currentTime
                consumeWindowMessage(true, true)
                return true
            else
                consumeWindowMessage(true, false)
                return true
            end
        end
        
        if inputIndex > #replacingString then
            consumeWindowMessage(true, true)
            return true
        end
    end
end)

function printChat(text)
    sampAddChatMessage(text, -1)
end

function keyStroke(char, delay)
    if char == nil then return end
    
    local key = tonumber(char)
    if not key or key < 0 or key > 9 then return end
    
    local vkCode = 0x30 + key
    
    lua_thread.create(function()
        setVirtualKeyDown(vkCode, true)
        wait(delay or 500)
        setVirtualKeyDown(vkCode, false)
        wait(10)
    end)
end

@chapo
 
Последнее редактирование:
Решение
попробуй

Lua:
local chatReplaceVar = -1
local inputIndex = 1
local replacingString = ""
local lastKeyTime = 0
local keyDelay = 100

function main()
    while not isSampAvailable() do wait(100) end
    
    sampRegisterChatCommand('replaceon', function(param)
        if param and param ~= "" then
            chatReplaceVar = tonumber(param)
            sampAddChatMessage('Подмена ввода включена: '..tostring(chatReplaceVar), -1)
        else
            chatReplaceVar = 123456
            sampAddChatMessage('Подмена ввода включена: 123456', -1)
        end
        replacingString = tostring(chatReplaceVar)
        inputIndex = 1
    end)
    
    sampRegisterChatCommand('replaceoff', function()
        chatReplaceVar = -1...

/vito/

Участник
8
2
попробуй

Lua:
local chatReplaceVar = -1
local inputIndex = 1
local replacingString = ""
local lastKeyTime = 0
local keyDelay = 100

function main()
    while not isSampAvailable() do wait(100) end
    
    sampRegisterChatCommand('replaceon', function(param)
        if param and param ~= "" then
            chatReplaceVar = tonumber(param)
            sampAddChatMessage('Подмена ввода включена: '..tostring(chatReplaceVar), -1)
        else
            chatReplaceVar = 123456
            sampAddChatMessage('Подмена ввода включена: 123456', -1)
        end
        replacingString = tostring(chatReplaceVar)
        inputIndex = 1
    end)
    
    sampRegisterChatCommand('replaceoff', function()
        chatReplaceVar = -1
        inputIndex = 1
        sampAddChatMessage('Подмена ввода отключена', -1)
    end)
    
    sampAddChatMessage('Скрипт загружен. Команды: /replaceon [число] /replaceoff', -1)
    
    while true do
        wait(0)
        if not sampIsChatInputActive() then
            inputIndex = 1
        end
    end
end

addEventHandler('onWindowMessage', function(msg, wparam, lparam)
    if chatReplaceVar == -1 or not sampIsChatInputActive() then return end
    
    if msg == 0x0102 then
        if wparam ~= 8 and wparam ~= 13 and wparam ~= 27 then
            consumeWindowMessage(true, true)
            return
        end
    end

    if msg == 0x0100 then
        if wparam == 8 then
            if inputIndex > 1 then
                inputIndex = inputIndex - 1
            end
            return
        end
        
        if wparam == 13 or wparam == 27 then
            inputIndex = 1
            return
        end

        if wparam < 32 then return end

        local currentTime = os.clock() * 1000
        
        if inputIndex <= #replacingString then
            if currentTime - lastKeyTime > keyDelay then
                local text = sampGetChatInputText()
                if #text < 128 then
                    local nextDigit = string.sub(replacingString, inputIndex, inputIndex)
                    sampSetChatInputText(text .. nextDigit)
                    inputIndex = inputIndex + 1
                    lastKeyTime = currentTime
                end
            end
            consumeWindowMessage(true, true)
        else
            consumeWindowMessage(true, true)
        end
    end
end)
 
  • Клоун
  • Нравится
Реакции: XRLM и Okak_Pon