AutoLogout when Mouse/Keyboard is unplugged

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
ivanz
Posts: 9
Joined: 16 Aug 2017, 17:04

AutoLogout when Mouse/Keyboard is unplugged

18 Aug 2017, 05:32

Hello,

I hope this isn't too much to ask for free, I have poor knowledge in programming or basic script creation..

I need a script which I can place in Windows 7 Startup folder and will do the following:

When Keyboard/Mouse disconnects (unplugged), launch the following script:

- Message box or command prompt window will pop-up containing the following:
- Computer will Logout in 60 Seconds (countdown) Press any key to continue..

- If true (any key is pressed), exit script..
- If false (no key is pressed), proceed to Logout.

You can change the Total Count of Mouse,Keyboard,HID into the actual Total Count of your PC using attached RawInput Devices Script.

Thank you sooooo much again, Guys! More Knowledge to you all! :superhappy:
Last edited by ivanz on 13 Jul 2018, 10:16, edited 3 times in total.
BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: AutoLogout when Mouse/Keyboard is unplugged

18 Aug 2017, 10:42

Ever entered a key with an intentionally unplugged keyboard? :lolno:
ivanz
Posts: 9
Joined: 16 Aug 2017, 17:04

Re: AutoLogout when Mouse/Keyboard is unplugged

18 Aug 2017, 19:28

BoBo wrote:Ever entered a key with an intentionally unplugged keyboard? :lolno:
Hello Sir,

The option is only when or if the keyboard was reconnected during the countdown..
ivanz
Posts: 9
Joined: 16 Aug 2017, 17:04

Re: AutoLogout when Mouse/Keyboard is unplugged

21 Aug 2017, 07:40

i'm sorry but i really have no idea on how that script works and how it would help me..

i just post request here hoping someone would help..

will try to watch some youtube guide..

thank you..
User avatar
Delta Pythagorean
Posts: 627
Joined: 13 Feb 2017, 13:44
Location: Somewhere in the US
Contact:

Re: AutoLogout when Mouse/Keyboard is unplugged

25 Aug 2017, 07:59

I have a feeling you can use DllCall to detect whether a device was removed but I don't think you can detect if a keyboard was unplugged unless it has it's own process and/or tray icon.

[AHK]......: v2.0.12 | 64-bit
[OS].......: Windows 11 | 23H2 (OS Build: 22621.3296)
[GITHUB]...: github.com/DelPyth
[PAYPAL]...: paypal.me/DelPyth
[DISCORD]..: tophatcat

User avatar
evilC
Posts: 4822
Joined: 27 Feb 2014, 12:30

Re: AutoLogout when Mouse/Keyboard is unplugged

25 Aug 2017, 08:43

Here you go, I wrote you a C# DLL that you can call from AHK to get this info.

Usage is simple, most of the code is using Lexikos' CLR lib to allow us to load the C# DLL

The DLL provides two functions: GetMouseCount() and GetKeyboardCount()

Code: Select all

#NoEnv
#SingleInstance force

#Include CLR.ahk

dllpath := "MouseKeyboardCount.dll"
if (!FileExist(dllpath)){
	msgbox % "MouseKeyboardCount: DLL file " dllpath " not found"
	ExitApp
}
asm := CLR_LoadLibrary(dllpath)
; Use CLR to instantiate a class from within the DLL
mkc := asm.CreateInstance("MouseKeyboardCount")

Loop {
	ToolTip % "Mice: " mkc.GetMouseCount() " Keyboards: " mkc.GetKeyboardCount() " @ " A_TickCount
	Sleep 1000
}
Two ZIPs are attached.

Demo.zip contains just the files you need to use this library

For those interested, the C# code is also super simple, I just use the SharpDX library:

Code: Select all

using SharpDX.DirectInput;

public class MouseKeyboardCount
{
    static private DirectInput directInput = new DirectInput();

    public int GetKeyboardCount()
    {
        var keyCount = 0;
        var devices = directInput.GetDevices();
        foreach (var deviceInstance in devices)
        {
            if (deviceInstance.Type == DeviceType.Keyboard)
            {
                keyCount++;
            }
        }
        return keyCount;
    }

    public int GetMouseCount()
    {
        var mouseCount = 0;
        var devices = directInput.GetDevices();
        foreach (var deviceInstance in devices)
        {
            if (deviceInstance.Type == DeviceType.Mouse)
            {
                mouseCount++;
            }
        }
        return mouseCount;
    }
}
Source.zip contains the Visual Studio project
Attachments
Source.zip
(168.28 KiB) Downloaded 56 times
Demo.zip
(160.09 KiB) Downloaded 70 times
ivanz
Posts: 9
Joined: 16 Aug 2017, 17:04

Re: AutoLogout when Mouse/Keyboard is unplugged

25 Aug 2017, 14:14

Thank you very much Sir.. I can see active mouse/keyboard detection whenever i run the compiled test.ahk inside the Demo.zip.

I'm sorry to bother, but can you please incorporate this little "screensaver" log-off program to call/run whenever the mouse/keyboard value gets to (0)?

Please.. :)
Attachments
Screensaver.zip
(184.31 KiB) Downloaded 60 times
just me
Posts: 9423
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: AutoLogout when Mouse/Keyboard is unplugged

27 Aug 2017, 05:32

evilC wrote:RawInput

Code: Select all

#NoEnv
DevCount := RI_GetDeviceCount()
MsgBox, 0, RawInput Devices, % "Total: " . DevCount.TC . "`n"
                             . "Mouse: " . DevCount.MC . "`n"
                             . "Keybd: " . DevCount.KC . "`n"
                             . "HID: "   . DevCount.HC
Exitapp
; ================================================================================================================================
; Raw Input -> msdn.microsoft.com/en-us/library/windows/desktop/ms645536(v=vs.85).aspx
; GetRawInputDeviceList - Enumerates the raw input devices attached to the system.
; ================================================================================================================================
RI_GetDeviceCount() {
   StructSize := A_PtrSize * 2 ; size of a RAWINPUTDEVICELIST structure
   DevCount := 0
   DllCall("GetRawInputDeviceList", "Ptr", 0, "UIntP", DevCount, "UInt", StructSize, "Int")
   If (DevCount) {
      VarSetCapacity(ListArr, StructSize * DevCount, 0) ; array of RAWINPUTDEVICELIST structures
      If (DllCall("GetRawInputDeviceList", "Ptr", &ListArr, "UIntP", DevCount, "UInt", StructSize, "Int") = DevCount) {
         Counters := {0: 0, 1: 0, 2: 0}
         Addr := &ListArr
         Loop, %DevCount% {
            Counters[NumGet(Addr + A_PtrSize, "UInt")]++
            Addr += StructSize
         }
         Return {HC: Counters[2], KC: Counters[1], MC: Counters[0], TC: DevCount}
      }
   }
   Return False
}
; ================================================================================================================================
?
ivanz
Posts: 9
Joined: 16 Aug 2017, 17:04

Re: AutoLogout when Mouse/Keyboard is unplugged

29 Aug 2017, 19:40

up..
ivanz wrote:Thank you very much Sir.. I can see active mouse/keyboard detection whenever i run the compiled test.ahk inside the Demo.zip.

I'm sorry to bother, but can you please incorporate this little "screensaver" log-off program to call/run whenever the mouse/keyboard value gets to (0)?

Please.. :)
garry
Posts: 3736
Joined: 22 Dec 2013, 12:50

Re: AutoLogout when Mouse/Keyboard is unplugged

30 Aug 2017, 02:16

I used script from 'just me'
have Logitech wireless mouse & keyboard
start script and remove USB-Receiver
check variables for mouse or keyboard or HID and then possible to start a program ( here msgbox )

Code: Select all

;-------- https://autohotkey.com/boards/viewtopic.php?f=5&t=35986 ---
/*
check every 5-seconds and show msgbox for 2-seconds

when  USB-Receiver is removed :
Total   = 4
Mouse   = 2
Keyboard= 2
HID     = 0

otherwise  :

Total   = 11
Mouse   =  3
Keyboard=  3
HID     =  5
*/

#NoEnv
#persistent
settimer,aa,5000,on
gosub,aa
return

aa:
DevCount := RI_GetDeviceCount()
HID1:=DevCount.HC
if (HID1=0)
{
msgbox, 262208,>>> HID=0, % "Total: " . DevCount.TC . "`n"
                             . "Mouse: " . DevCount.MC . "`n"
                             . "Keybd: " . DevCount.KC . "`n"
                             . "HID: "   . DevCount.HC,2
return
}
else
{
msgbox, 262208,RawInput Devices, % "Total: " . DevCount.TC . "`n"
                             . "Mouse: " . DevCount.MC . "`n"
                             . "Keybd: " . DevCount.KC . "`n"
                             . "HID: "   . DevCount.HC,2
return
}
return
esc::exitapp

; ================================================================================================================================
; Raw Input -> msdn.microsoft.com/en-us/library/windows/desktop/ms645536(v=vs.85).aspx
; GetRawInputDeviceList - Enumerates the raw input devices attached to the system.
; ================================================================================================================================
RI_GetDeviceCount() {
   StructSize := A_PtrSize * 2 ; size of a RAWINPUTDEVICELIST structure
   DevCount := 0
   DllCall("GetRawInputDeviceList", "Ptr", 0, "UIntP", DevCount, "UInt", StructSize, "Int")
   If (DevCount) {
      VarSetCapacity(ListArr, StructSize * DevCount, 0) ; array of RAWINPUTDEVICELIST structures
      If (DllCall("GetRawInputDeviceList", "Ptr", &ListArr, "UIntP", DevCount, "UInt", StructSize, "Int") = DevCount) {
         Counters := {0: 0, 1: 0, 2: 0}
         Addr := &ListArr
         Loop, %DevCount% {
            Counters[NumGet(Addr + A_PtrSize, "UInt")]++
            Addr += StructSize
         }
         Return {HC: Counters[2], KC: Counters[1], MC: Counters[0], TC: DevCount}
      }
   }
   Return False
}
; ================================================================================================================================

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Google [Bot], iamMG and 127 guests