我如何检测 Godot3.1 中打开的程序?

How can i detect opened programs in Godot3.1?

我想检测,就是chrome打开了,但是我不知道怎么检测。

这是一个程序,可以检测我弟弟看 YT 视频的次数。

Godot 不允许退出 "user://"

如果你有 Windows 10 作为操作系统,你可以调用 Godot 的 powershell 来制作它。 在 powershell 中,要获取 chrome 进程的列表,请使用 get-process chrome ,您将看到如下内容:

    Handles  NPM(K)    PM(K)      WS(K)     CPU(s)     Id  SI ProcessName
    -------  ------    -----      -----     ------     --  -- -----------
    343      19    31404      57972       0,88   2664   2 chrome
    259      17    22972      43920       0,34   2972   2 chrome
    529      29    76956      65512       1,00   3576   2 chrome
    238      17    24148      46548       0,55   5480   2 chrome
    219      15    13084      23128       0,22   7676   2 chrome
    136      11     1992       8724       0,05   7924   2 chrome
    161       9     1676       6484       0,03  10200   2 chrome
    230      16    16504      33064       0,17  13252   2 chrome
    415      21    14372      30508       5,45  14836   2 chrome
    195    8717    44248      27944       0,75  15520   2 chrome
   1290      49    72020     129948      14,66  17652   2 chrome

如果写get-process chrome | measure-object -line可以得到行数:

    Lines Words Characters Property
    ----- ----- ---------- --------
       11

最后,如果您写 get-process chrome | measure-object -line | select Lines -expandproperty Lines,您将只会看到行数:

11

现在,在 Godot 中应用这个:

func _is_chrome_active() -> bool:
    var chrome_active = false
    if OS.get_name() == "Windows": # Verify that we are on Windows
        var output = []
        # Execute "get-process" in powershell and save data in "output":
        OS.execute('powershell.exe', ['/C', "get-process chrome | measure-object -line | select Lines -expandproperty Lines"], true, output)   
        var result = output[0].to_int()
        chrome_active = result > 0    # If there is more than 0 chrome processes, it will be true
        print("Number of chrome processes: " + str(result))
    return chrome_active

这里使用OS.execute打开powershell并发送命令。结果将作为 Array 保存在 output 变量中。从中取出第一个(也是唯一的)元素,并用 var result = output[0].to_int() 行将其转换为数字。之后,将该值与 0 进行比较,以了解是否有某个 chrome 进程正在执行。 此函数 returns 如果有一些 chrome 进程处于活动状态则为真,否则为假。

现在,您可以从计时器调用它并计算 chrome 打开时经过的时间。