AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

USB device connected/disconnected notification?
Goto page 1, 2, 3  Next
 
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Ask for Help
View previous topic :: View next topic  
Author Message
evl



Joined: 24 Aug 2005
Posts: 1239

PostPosted: Thu Mar 30, 2006 10:33 pm    Post subject: USB device connected/disconnected notification? Reply with quote

Does Windows broadcast a message to all processes when a usb device is plugged in/removed so that a script could just use OnMessage() to catch this (and then perform a check) instead of using a timer to constantly check?

The device in question is a usb microphone ("usb audio" device) so maybe it's different to detecting usb drives.

I've seen this thread (and replied about where I found device info is stored in the registry, in case it helps anyone else):
http://www.autohotkey.com/forum/viewtopic.php?t=6618
(a timer can be used to check this location quite well, but I'd prefer not to check the registry every second - 86400 times a day Smile ) - plus it'd react faster to a message than a timer.
Back to top
View user's profile Send private message
Paulo
Guest





PostPosted: Fri Mar 31, 2006 1:41 pm    Post subject: Reply with quote

I don't know for sure if it will work with mics but it works with all my usb devices.

OnMessage(0x219, "notify_change")
Return

notify_change(wParam, lParam, msg, hwnd)
{
MsgBox, %wParam% %lParam% %msg% %hwnd%
}
Back to top
Kevin4875



Joined: 21 Mar 2006
Posts: 5

PostPosted: Fri Mar 31, 2006 2:08 pm    Post subject: Reply with quote

Wow, how did you've done that?

It works with me too!
Back to top
View user's profile Send private message
evl



Joined: 24 Aug 2005
Posts: 1239

PostPosted: Fri Mar 31, 2006 2:18 pm    Post subject: Reply with quote

Excellent! I'd overlooked that message - it catches both connection and disconnection.

For anyone else that needs this in the future:

Here's the MSDN info on WM_DEVICECHANGE:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/devio/base/wm_devicechange.asp
Back to top
View user's profile Send private message
Goodhue
Guest





PostPosted: Fri Mar 31, 2006 6:13 pm    Post subject: Reply with quote

What about firewire devices? Is there a different message to use?
Back to top
evl



Joined: 24 Aug 2005
Posts: 1239

PostPosted: Fri Mar 31, 2006 6:39 pm    Post subject: Reply with quote

It probably works too:

Quote:
The WM_DEVICECHANGE device message notifies an application of a change to the hardware configuration of a device or the computer.


Give it a try Wink
Back to top
View user's profile Send private message
evl



Joined: 24 Aug 2005
Posts: 1239

PostPosted: Fri Mar 31, 2006 6:43 pm    Post subject: Reply with quote

Also, the wParam value I get is not mentioned on the MSDN page:
0x0007 = DBT_DEVNODES_CHANGED

It was defined in the Dbt.h file (as mentioned on the site) which contains quite a bit of info.
Back to top
View user's profile Send private message
Tekl



Joined: 24 Sep 2004
Posts: 813
Location: Germany

PostPosted: Wed Jul 19, 2006 9:17 am    Post subject: Reply with quote

Hmmm... it seems not always to work with multi card readers when I plug in or remove media cards. Sometimes I'll get a message and sometimes not.
_________________
Tekl
Back to top
View user's profile Send private message Visit poster's website
Geek10000



Joined: 03 Feb 2008
Posts: 5

PostPosted: Sun Feb 10, 2008 7:54 am    Post subject: How to detect if monitor is present Reply with quote

Hi. Thak you for the easy oode. I have laptop computer which I use partly at work, partly at home. When I am at home, I use it with an external monitor. Is there anyway to write a scrip that detect if there is a vga cable present in the slot - so that my laptop will change to external output when I connect it at home? Now I have to connect it, wait for half a minute, then press ctrl + alt + f1. Would be brilliant, but I dont know where to start. I have benne searching all the internet without any answears. Thank you in advance. Best.
Back to top
View user's profile Send private message
[VxE]



Joined: 07 Oct 2006
Posts: 1484

PostPosted: Sun Feb 10, 2008 9:23 am    Post subject: Reply with quote

Robin wrote:
Holy Bumpzilla, Batman!

I believe you may be interested in SysGet, outputvar, MonitorCount.

i.e.:
Code:
SysGet, HowMany, MonitorCount
msgfb := "There " ((HowMany-1) ? "are" : "is") " currently " HowMany
 . " monitor" ((HowMany-1) ? "s" : "") " connected to your system."
msgbox % msgfb
return

_________________
My Home Thread
More Common Answers: [1]. It's in the FAQ [2]. Ternary ( a ? b : c ) guide [3]. Post code inside [code][/code] tags !
Back to top
View user's profile Send private message
Geek10000



Joined: 03 Feb 2008
Posts: 5

PostPosted: Sun Feb 10, 2008 4:35 pm    Post subject: Detect VGA immeadtely Reply with quote

Thank you so much. But is it possible to have this code to autmatically detect WHEN I plug in my external screen? So that the computer will send the keystrokes alt + ctrl + F1?

Thank you so much.
Back to top
View user's profile Send private message
[VxE]



Joined: 07 Oct 2006
Posts: 1484

PostPosted: Wed Feb 13, 2008 4:37 am    Post subject: Reply with quote

Code:
SetTimer, CheckForTwoMonitors, 400
CheckForTwoMonitors:
SysGet, HowMany, MonitorCount
If HowMany = %PrevMany%
   return
If (( PrevMany := HowMany ) = 2)
   Send ^!{F1}
return
How 'bout that?
_________________
My Home Thread
More Common Answers: [1]. It's in the FAQ [2]. Ternary ( a ? b : c ) guide [3]. Post code inside [code][/code] tags !
Back to top
View user's profile Send private message
Geek10000



Joined: 03 Feb 2008
Posts: 5

PostPosted: Wed Feb 13, 2008 1:05 pm    Post subject: Detect monitors Reply with quote

Thank you! I will check as soon as I get home. But when I run this code on my laptop, the messagebox always tells me it is 1 monitor connected. Even if I have plugged in my external monitor in the vga-slot?
Back to top
View user's profile Send private message
[VxE]



Joined: 07 Oct 2006
Posts: 1484

PostPosted: Wed Feb 13, 2008 8:17 pm    Post subject: Reply with quote

You need to have the second monitor enabled in your display properties for it to work properly. Otherwise I don't think AHK can detect a monitor being plugged in.
_________________
My Home Thread
More Common Answers: [1]. It's in the FAQ [2]. Ternary ( a ? b : c ) guide [3]. Post code inside [code][/code] tags !
Back to top
View user's profile Send private message
M3CSL



Joined: 21 Jan 2008
Posts: 35
Location: Germany

PostPosted: Wed Feb 13, 2008 11:43 pm    Post subject: Reply with quote

Paulo wrote:
OnMessage(0x219, "notify_change")
Return

notify_change(wParam, lParam, msg, hwnd)
{
MsgBox, %wParam% %lParam% %msg% %hwnd%
}


Is it possible to get the drive letter of the new device and open its content in Windows Explorer?
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Ask for Help All times are GMT
Goto page 1, 2, 3  Next
Page 1 of 3

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group