Toggle SCROLL LOCK when screensaver activates

十年热恋 提交于 2019-12-25 06:08:08

问题


I wish to make an application that runs in the background at all times in windows that toggles scroll lock when my computer's screensaver enables, then toggle it back after it disables, if possible.

The reason is that I have a keyboard that lights up depending on the state of the scroll lock light. It would be cool to have the keyboard turn off the lights automatically when im not using it.

I know some code in VBscript, some in Python, and lots in VB. I have tried using a code snippet from this and turning it into this:

strComputer = "computername"
Set wshShell =wscript.CreateObject("WScript.Shell")
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colProcesses = objWMIService.ExecQuery("Select * from Win32_Process")

Do
  WScript.Sleep 300
  For Each objProcess In colProcesses
    If Right(objProcess.Name, 4) = ".scr" Then
      wshShell.SendKeys "{SCROLLLOCK}"
    End If 
  Next
Loop

which works only if the program runs when the screensaver is already on. Please help me find out how I can make this check every 300 ticks if the screensaver is on, or tell me how in one of the other languages listed in the title.


回答1:


The result of a WMI query reflects the state when the query was run. It doesn't automatically refresh and thus won't pick up any processes that were launched after its initial run. Move the query inside the outer loop so it's re-run with every iteration:

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

Do
  WScript.Sleep 300
  Set colProcesses = objWMIService.ExecQuery("Select * from Win32_Process")
  For Each objProcess In colProcesses
    If Right(objProcess.Name, 4) = ".scr" Then
      wshShell.SendKeys "{SCROLLLOCK}"
    End If 
  Next
Loop


来源:https://stackoverflow.com/questions/33204993/toggle-scroll-lock-when-screensaver-activates

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!