Last active
April 22, 2024 11:40
-
-
Save cobryan05/8e191ae63976224a0129a8c8f376adc6 to your computer and use it in GitHub Desktop.
Revisions
-
cobryan05 revised this gist
Apr 17, 2024 . 1 changed file with 40 additions and 14 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,28 +1,54 @@ # 2024-04-17: Stole changese from @Timmo on stackoverflow to get this working with Windows Apps import winreg class WebcamDetect: REG_KEY = winreg.HKEY_CURRENT_USER WEBCAM_REG_SUBKEY = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\CapabilityAccessManager\\ConsentStore\\webcam\\" WEBCAM_TIMESTAMP_VALUE_NAME = "LastUsedTimeStop" def __init__(self): self._regKey = winreg.OpenKey( WebcamDetect.REG_KEY, WebcamDetect.WEBCAM_REG_SUBKEY ) def getActiveApps(self): """Returns a list of apps that are currently using the webcam.""" def get_subkey_timestamp(subkey) -> int: """Returns the timestamp of the subkey""" try: value, _ = winreg.QueryValueEx(subkey, WebcamDetect.WEBCAM_TIMESTAMP_VALUE_NAME) return value except OSError: pass return None active_apps = [] try: key = winreg.OpenKey(WebcamDetect.REG_KEY, WebcamDetect.WEBCAM_REG_SUBKEY) # Enumerate over the subkeys of the webcam key subkey_count, _, _ = winreg.QueryInfoKey(key) # Recursively open each subkey and check the "LastUsedTimeStop" value. # A value of 0 means the camera is currently in use. for idx in range(subkey_count): subkey_name = winreg.EnumKey(key, idx) subkey_name_full = f"{WebcamDetect.WEBCAM_REG_SUBKEY}\\{subkey_name}" subkey = winreg.OpenKey(WebcamDetect.REG_KEY, subkey_name_full) if subkey_name == "NonPackaged": # Enumerate over the subkeys of the "NonPackaged" key subkey_count, _, _ = winreg.QueryInfoKey(subkey) for np_idx in range(subkey_count): subkey_name_np = winreg.EnumKey(subkey, np_idx) subkey_name_full_np = f"{WebcamDetect.WEBCAM_REG_SUBKEY}\\NonPackaged\\{subkey_name_np}" subkey_np = winreg.OpenKey(WebcamDetect.REG_KEY, subkey_name_full_np) if get_subkey_timestamp(subkey_np) == 0: active_apps.append(subkey_name_np) else: if get_subkey_timestamp(subkey) == 0: active_apps.append(subkey_name) winreg.CloseKey(subkey) winreg.CloseKey(key) except OSError: pass return active_apps def isActive(self): return len(self.getActiveApps()) > 0 -
cobryan05 created this gist
Feb 9, 2022 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,28 @@ import winreg class WebcamDetect: REG_KEY = winreg.HKEY_CURRENT_USER WEBCAM_REG_SUBKEY = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\CapabilityAccessManager\\ConsentStore\\webcam\\NonPackaged" WEBCAM_TIMESTAMP_VALUE_NAME = "LastUsedTimeStop" def __init__(self): self._regKey = winreg.OpenKey( WebcamDetect.REG_KEY, WebcamDetect.WEBCAM_REG_SUBKEY ) def getActiveApps(self): activeApps = [] # Enumerate over the subkeys of the webcam key subkeyCnt, valueCnt, lastModified = winreg.QueryInfoKey( self._regKey ) for idx in range(subkeyCnt): subkeyName = winreg.EnumKey( self._regKey, idx ) subkeyFullName = f"{WebcamDetect.WEBCAM_REG_SUBKEY}\\{subkeyName}" # Open each subkey and check the 'stopped' timestamp value. A value of 0 implies the camera is in use. subkey = winreg.OpenKey( WebcamDetect.REG_KEY, subkeyFullName ) stoppedTimestamp, _ = winreg.QueryValueEx( subkey, WebcamDetect.WEBCAM_TIMESTAMP_VALUE_NAME ) if 0 == stoppedTimestamp: activeApps.append( subkeyName.replace("#", "/" ) ) return activeApps def isActive(self): return len(self.getActiveApps()) > 0