Sign in to follow this  
Followers 0
ZiggyStardust

Need a "HotKeySet" when change in hardware is detected

11 posts in this topic

I have a little script written that does nothing but check for the presence or absence of a usb dongle and then acts accordingly based on its presence or absence

while 1

checkfordongle()

if present ...

if ansent...

yada yada

WEnd

It sure seems like a waste of ticks ;) Is there a "HotKetSet" or something I can use (interrupt) that will only invoke my program if a change in hardware is detected? Windows XP or WIndows 7.

Basically I want my program to run in the background and only do the "checkfordongle" if a change in hardware is detected. No point in doing it once per loop if I can get away with doing it only if a change in hardware occurs.

Thanks!

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

No matter what you write you will always have to have it the loop to check for a change. There might be a better way to check for Removable Devices but this is the best I could come up with. Might be a DllCall that might do a better job but I couldn't find one.

Global $CurrentDevices = CheckRemovable()

While 1
    $NumberofDevices = CheckRemovable()
    If $NumberofDevices > $CurrentDevices Then
        $CurrentDevices = $NumberofDevices
;~      Func to run if device is inserted
    ElseIf $NumberofDevices < $CurrentDevices Then
        $CurrentDevices = $NumberofDevices
;~      Func to run if device is removed
    EndIf
    Sleep(100)
WEnd

Func CheckRemovable()
    Local $RemovableDevices = 0
    For $i = 65 To 90
        $Drive = Chr($i) & ":"
        $DriveType[$i] = DriveGetType($Drive)
        If $DriveType[$i] = "Removable" Then $RemovableDevices += 1
    Next
    Return($RemovableDevices)
EndFunc   ;==>DriveInfo
Edited by Rogue5099

Share this post


Link to post
Share on other sites

No matter what you write you will always have to have it the loop to check for a change.

No? A quick Google search and I found WM_DEVICECHANGE and RegisterDeviceNotification(). Another quick search here and there are examples for both.

Also it seems that you duplicated DriveGetDrive() behaviour. And your $DriveType does nothing.

Thanks to the latest alpha, your function really only needs to be two lines.

MsgBox(0, "", CheckRemovable())

Func CheckRemovable()
    vRet = DriveGetDrive("REMOVABLE")
    Return IsArray(vRet) ? vRet[0] : 0   ;Req. AutoIt 3.3.9.5
EndFunc

;)

Share this post


Link to post
Share on other sites

Thanks to the latest alpha, your function really only needs to be two lines.

MsgBox(0, "", CheckRemovable())

Func CheckRemovable()
    vRet = DriveGetDrive("REMOVABLE")
    Return IsArray(vRet) ? vRet[0] : 0   ;Req. AutoIt 3.3.9.5
EndFunc

;)

MsgBox(0, "", CheckRemovable())

Func CheckRemovable()
    Return IsArray(DriveGetDrive("REMOVABLE")) ? DriveGetDrive("REMOVABLE")[0] : 0
EndFunc

DONT HURT ME! ;D

Share this post


Link to post
Share on other sites

MsgBox(0, "", CheckRemovable())

Func CheckRemovable()
    Return IsArray(DriveGetDrive("REMOVABLE")) ? DriveGetDrive("REMOVABLE")[0] : 0
EndFunc

DONT HURT ME! ;D

Can I pinch you atleast? lol

I could have done something silimar but I didn't want to run DriveGetDrive() twice.

Actually, that gives me an idea

MsgBox(0, "", CheckRemovable())

Func CheckRemovable()
    Return Assign("vRet", DriveGetDrive("REMOVABLE")) * IsArray(vRet) ? vRet[0] : 0
EndFunc

;)

Share this post


Link to post
Share on other sites

MsgBox(0, "", CheckRemovable())

Func CheckRemovable()
    Return Assign("vRet", DriveGetDrive("REMOVABLE")) * IsArray(vRet) ? vRet[0] : 0
EndFunc

When I try this I get a syntax error at the "?". Not to mention you forgot the "$" in front of the vRet's. But even with that it's not working for me. I'm interested in this as well because I was using DriveGetDrive multiple times in 1 of my scripts and this has thread has helped me clean that up.

MsgBox(0, "", CheckRemovable())

Func CheckRemovable()
    Return Assign("vRet", DriveGetDrive("REMOVABLE")) * IsArray($vRet) ? $vRet[0] : 0
EndFunc

Share this post


Link to post
Share on other sites

That code and others above it will only work with the latest Alpha release of AutoIt, which was mentioned in the first post with that code in it.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

Same here, seems that either AU3Check isn't working, or that code is wrong. ;)

EDIT: I ran the code without running AU3Check, and it ran ok, seems that it's an issue with AU3Check and the new code syntax.

Edited by BrewManNH

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

#10 ·  Posted

3.3.9.5 doesn't have an installer. And it's an alpha, au3check doesn't have to support everything there. Just run AutoIt. The code is fine, see the changelog for tertiary and optional $.

Share this post


Link to post
Share on other sites

#11 ·  Posted (edited)

Even if the alpha supports teritary (()?;) avoid contructs like this one. It is really bad coding style.

Return Assign("vRet", DriveGetDrive("REMOVABLE")) * IsArray($vRet) ? $vRet[0] : 0
Only do one task at a time, no assign and return in the same line. At maximum add a call to SetError:

Return IsArray(vRet) ? vRet[0] : 0

Return IsArray(vRet) ? vRet[0] : SetError(1, 0, 0)

And to contribute to the topic: Here is a discussion with code for WM_DEVICECHANGE: http://autoitscript.com/forum/topic/120293-solved-how-to-use-registerdevicenotification/

Edited by ProgAndy

*GERMAN* [note: you are not allowed to remove author / modified info from my UDFs]My UDFs:[_SetImageBinaryToCtrl] [_TaskDialog] [AutoItObject] [Animated GIF (GDI+)] [ClipPut for Image] [FreeImage] [GDI32 UDFs] [GDIPlus Progressbar] [Hotkey-Selector] [Multiline Inputbox] [MySQL without ODBC] [RichEdit UDFs] [SpeechAPI Example] [WinHTTP]UDFs included in AutoIt: FTP_Ex (as FTPEx), _WinAPI_SetLayeredWindowAttributes

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0