如何使用相同的按键关闭打开它的 GUI(jQuery)

所以,我正在尝试对现有的 FiveM 库存脚本进行一些编辑,该脚本使用 Javascript 打开 GUI 并将其关闭

打开库存时一部分 lua 代码:

if not IsPlayerDead(PlayerId()) then
            DisableControlAction(0, 37, true)
            if IsDisabledControlJustPressed(1, Config.OpenControl) and GetLastInputMethod(0) then
            -- Config.OpenControl 就是关闭键 (TAB ↹)
                    openInventory()

Javascript 代码片段:

(我尝试添加一个变量并在打开 GUI 时将其设置为 True)

// var inventoryOpen = true;

window.addEventListener(“message”,function(event){
    if(event.data.action ==“display”){
        // inventoryOpen = true;
        type = event.data.type
        disabled = false;

(当调用 closeinventory()函数时尝试将变量设置为 false)

function closeInventory() {
    $(“.UI”)。fadeOut();
    $ .post(“http://pb-inventory/NUIFocusOff”,JSON.stringify({}));
    // inventoryOpen = false;
}

(这是按下关闭按钮时的情况)

$(“body”)。on(“keyup”,function(key){
        if(Config.closeKeys.includes(key.which)){ // Config.closeKeys 这里是 9,即 TAB 键 ↹
            // if(inventoryOpen == true){ 
                closeInventory();
            // }
        }
    });

所以当我尝试上面的注释代码时,GUI 开始出现故障,并在打开后一直关闭。

点赞