Why doesn't my MacBook recognize my USB keyboard when it wakes from sleep?

I have 13-inch, early 2008 MacBook running OS X 10.7.3, and recently, the trackpad started developing some problems, so I attached an external keyboard and mouse.

It works great, except that when the computer wakes up from sleep, about 50% of the time the keyboard is unresponsive until I unplug and replug the USB connection. Any idea why this would be?

The keyboard and mouse are attached directly to the computer, not going through any USB hub.


I sometimes have this problem with my built-in keyboard (and trackpad). It seems to be related to static electricity build-up (in me). In dry environments prone to static generation (e.g., carpeted offices, fabric-covered chairs, etc.), I always try to ground myself on something (say, a filing cabinet or a metal wall) after I sit down but before I touch the mac. If I forget and I zap the keyboard when I start to type, there's a very good chance that the keyboard and trackpad will be unresponsive. Based on my recovery action, I'm guessing that the macbook's internal USB controller has sensed the transient voltage spike as an over-current condition and has shut down (suspended) the offending devices on the associated port.

To recover the keyboard and trackpad, I either plug in an external keyboard (or vnc in via an ssh tunnel) and run the USB Prober app (part of the Developers tools, under /Developer/Applications/Utilities). Inside Prober with the Bus Probe tab selected, I then check Probe suspended devices and hit the Refresh button; my built-in keyboard and trackpad immediately start working again. My assumption is that the probe causes the suspended devices to re-negotiate their bus membership --which, if I remember the USB protocol, means that they are essentially new devices and not subject to the previous suspension -- and lets things go back to normal.

This doesn't happen often enough for me to automate the recovery, but I'm guessing there are associated system events/messages that could be used to drive an applescript-controlled probe.