Skip to content

Instantly share code, notes, and snippets.

@cobryan05
Last active April 22, 2024 11:40
Show Gist options
  • Select an option

  • Save cobryan05/8e191ae63976224a0129a8c8f376adc6 to your computer and use it in GitHub Desktop.

Select an option

Save cobryan05/8e191ae63976224a0129a8c8f376adc6 to your computer and use it in GitHub Desktop.

Revisions

  1. cobryan05 revised this gist Apr 17, 2024. 1 changed file with 40 additions and 14 deletions.
    54 changes: 40 additions & 14 deletions webcamDetect.py
    Original 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\\NonPackaged"
    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):
    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("#", "/" ) )
    """Returns a list of apps that are currently using the webcam."""

    return activeApps
    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
  2. cobryan05 created this gist Feb 9, 2022.
    28 changes: 28 additions & 0 deletions webcamDetect.py
    Original 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