Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

UsbKey_L


  • Please log in to reply
8 replies to this topic
GeekDude
  • Members
  • 354 posts
  • Last active: Oct 03 2014 08:32 PM
  • Joined: 23 Nov 2009
This is my take on a USB lock.
It requires AHK_L

Instructions:
Run script
Insert device to be a key (works will all types of removable memory, including flash drives and SD cards)
Hit yes
Remove device
Watch screen get locked
Try to get around it without shutting down
Plug device in
Watch screen get unlocked
hit yes or no depending if you still want to be able to lock it when you remove the device
exit script

Uses code from these people and pages:
<!-- m -->http://www.autohotke...topic68826.html<!-- m --> - fures - Locking code
<!-- m -->http://www.autohotke...topic53493.html<!-- m --> - rktboy - On device change
<!-- m -->http://www.autohotke...topic74542.html<!-- m --> - Cristi® - Get drive list

The actual code:
#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.

Hotkey, LWin, Lock, Off
Hotkey, RWin, Lock, Off
Hotkey, LCtrl, Lock, Off
Hotkey, RCtrl, Lock, Off
Hotkey, LAlt, Lock, Off
Hotkey, RAlt, Lock, Off

List := {} ; Make 'List' an associative array

OnMessage(0x219, "notify_change") ; when a drive changes, notify me

DriveGet, Drivelist, list, Removable ; get a list of devices with removeable storage that are connected when the script starts
Loop, parse, Drivelist ; parse it
{
	Letter := A_LoopField 
	Drive := Letter . ":\"
	DriveGet, Serial, Serial, %Drive%
	DriveGet, Label, Label, %Drive%
	List[Letter] := [Serial, Label] ; Add the Drive to the array
}
return

notify_change(wParam, lParam, msg, hwnd) 
{
	SetTimer, Check, -1500 ; When a device changes, put the timer back to the beginning. This means that if the timer is not finished, it will not run until the the new time runs out
}

Check:
NewList := {} ; reset newlist
Changed := {} ; reset changed

DriveGet, Drivelist, list, Removable
Loop, parse, Drivelist
{
	Letter := A_LoopField
	Drive := Letter . ":\"
	DriveGet, Serial, Serial, %Drive%
	DriveGet, Label, Label, %Drive%
	NewList[Letter] := [Serial, Label]
}

For letter, SubArray in NewList ; loop through the new list
{
	If (List[letter]) ; if the drive is in the old list
		List.Remove(letter) ; delete it, it has not been changed
	else ; if not
		Changed[letter] := [1, SubArray[1], SubArray[2]] ; add it to the changed list as "connected"
}
For letter, SubArray in List ; because we deleted add the entries still in the new list
	Changed[letter] := [0, SubArray[1], SubArray[2]] ; the only ones left have been removed. add them to the changed list as "removed"
List := NewList ; Set the list to the new list

for Letter, Connected in Changed ; loop through the changed
{
	if (Changed[Letter, 1] = 1) ; If a device was connected
	{
		if key ; If there is already a registered device
		{
			if locked ; If your computer is locked
			{
				if (Changed[Letter, 2]=key) ; If the device matches the key
				{
					Locked := ; Set your computer to unlocked
					gosub, Locked ; Unlock your computer
					MsgBox, 4, UsbKey, % "Your computer has been unlocked.`nWould you like to unregister """ . Changed[Letter, 3] . """ as a key?"
					IfMsgBox, Yes ; If the user wants to unregister his device as a key
						key := ; Unregister it
				}
			}
			else ; If your computer is not locked
				MsgBox,, UsbKey, There is already a key inserted in to the computer.
		}
		else ; If there is not already a registered device
		{
			MsgBox, 4, UsbKey, % "A removeable mass storage device has been connected. Would you like to register """ . Changed[Letter, 3] . """ as a key?"
			IfMsgBox, Yes ; If the user wants to register the device
				key := Changed[Letter, 2] ; register it as a key
		}
	}
	else if (key && Changed[Letter, 2]=key) ; if a device was removed, and it matches the key
	{
		locked := 1 ; lock the computer
		gosub, Locked
	}
}
Return

Locked:
if Locked ; If the computer needs to be locked
{
	Hotkey, LWin, On
	Hotkey, RWin, On
	Hotkey, LCtrl, On
	Hotkey, RCtrl, On
	Hotkey, LAlt, On
	Hotkey, RAlt, On
	SetTimer, Lock, 50
	
}
else if not Locked ; if it needs to be unlocked
{
	SetTimer, Lock, Off
	Hotkey, LWin, Off
	Hotkey, RWin, Off
	Hotkey, LCtrl, Off
	Hotkey, RCtrl, Off
	Hotkey, LAlt, Off
	Hotkey, RAlt, Off
	WinShow, ahk_class Shell_TrayWnd
	WinClose, Full_Screen_Gui
}
Return

Lock:
WinSet,AlwaysOnTop,OFF,Windows Task Manager
WinSet,Bottom,,Windows Task Manager
WinHide, ahk_class Shell_TrayWnd
Gui, -Caption
Gui, Color, Black
Gui, Show, H%A_ScreenHeight% W%A_ScreenWidth% X0 Y0, Full_Screen_Gui
return

^esc::
ExitApp
return

Hope you enjoy!

Nazzal
  • Members
  • 265 posts
  • Last active: Jan 19 2014 04:31 PM
  • Joined: 19 Nov 2010
Cool Posted Image

DataLife
  • Members
  • 993 posts
  • Last active: Oct 23 2014 09:30 PM
  • Joined: 27 Apr 2008
This is great.

Although, while my screen was locked I pressed some keys and one of my Always on Top Guis appeared.

Can Always on Top guis be prevented from being shown while the screen is locked?

DataLife
  • Members
  • 993 posts
  • Last active: Oct 23 2014 09:30 PM
  • Joined: 27 Apr 2008
I found an issue.

While my screen is locked and I press Ctrl and Alt and release them then insert the key the Ctrl and Alt are still held down.

I noticed this because with Windows 7, Ctrl-Alt and an arrow screen rotates the screen to be upside down or turned horizontal.

It took me awhile to figure out why my screen was turning upside down with my down arrow key. I thought a joke was added to one of the recent scripts I downloaded from the Autohotkey forum.

GeekDude
  • Members
  • 354 posts
  • Last active: Oct 03 2014 08:32 PM
  • Joined: 23 Nov 2009
DataLife, some graphics drivers have key combinations for switching the screen upside-down and sideways. <!-- m -->http://forums.techgu... ... pside.html<!-- m -->

DataLife
  • Members
  • 993 posts
  • Last active: Oct 23 2014 09:30 PM
  • Joined: 27 Apr 2008

DataLife, some graphics drivers have key combinations for switching the screen upside-down and sideways. <!-- m -->http://forums.techgu... ... pside.html<!-- m -->

Yes you are correct, but USB lock does not release the Ctrl and the Alt keys if they are pressed while the screen is locked.

So all kinds of weird things happen after my screen is unlocked because those two keys are still held down.

One of those weird things is any arrow key rotates my screen because the Ctrl and the Alt keys are still held down and the 3rd key is an arrow key.

Ctrl Alt Down arrow flips my screen (this is normal with windows 7), but after pressing Ctrl Alt while my screen is locked by USB lock and then unlocked I only need to press the down key and it flips my screen. This is because the Ctrl and Alt keys are never released when my screen becomes unlocked by USB lock.

GeekDude
  • Members
  • 354 posts
  • Last active: Oct 03 2014 08:32 PM
  • Joined: 23 Nov 2009
I tried reproducing, but it did not work for me (maybe I did it wrong?). anyways, this could be a problem with HotKey, <Key>, Off

DataLife
  • Members
  • 993 posts
  • Last active: Oct 23 2014 09:30 PM
  • Joined: 27 Apr 2008

I tried reproducing, but it did not work for me (maybe I did it wrong?). anyways, this could be a problem with HotKey, <Key>, Off

I must of had some kind of conflict with another program. I am not able to reproduce that issue again either.

Sorry for any troubles.

R3gX
  • Members
  • 307 posts
  • Last active: Dec 29 2013 04:50 PM
  • Joined: 28 Feb 2011
Hi!
I like your script but it should be used also for multiple screens and block the RButton!