WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

Functions

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

WaitForKey Issues

 Keywords:  

Question:

We are finding erratic behavior with the WaitForKey() and WaitForKeyEx() functions.

We are using WinBatch version 2008A.

On our desktops (Dell GX270 with XP SP3) they both seem to work fine. On our laptops (Dell D820 with XP SP3) these functions exhibit the following behaviors using the WinBatch help file example:

  1. The functions don't respond at all.
  2. The functions work but only after many key presses.
  3. The functions return the wrong key.

The laptop keyboard seems to be functioning correctly otherwise.

IsKeyDown() seems to work fine on both laptops and desktops. For our current use IsKeyDown() is suitable so this post is mostly curiosity.

Thanks for any insight.

Answer:

WaitForKey and WaitForKeyEx simply look at a snapshot of the windows message stream. The key has to be pressed when WinBatch checks. It is a polling operation - fast hits can be missed. Make sure you are holding down the key when testing.

Is it possible you have some utility running on the system that attempts to interfere with what it thinks is a 'key logger' program?

I suspect some other application is also attempting to log keystrokes. Due to the pre-emptive multitasking nature of Windows, another application can look for the "recently pressed" key instead of your application. Causing winbatch to miss the keypresses.

Or maybe some spyware or anti-virous utility is installed that attempts to interfere with this type of operation.

It might be interesting to look at a list of all the running processes on these laptops.

WorkAround:
Here is some code that uses different method of monitoring keystrokes.

;Slighty different method to grab the keys.
#DefineFunction GetKeyboardState(buf)
   sDLLName = StrCat(DirWindows(1), "user32.dll")
   DllCall(sDLLName, long:"GetKeyboardState", lpbinary:buf)
   BinaryEodSet(buf, 256)
   Return buf
#EndFunction

;==============================
;start
;==============================
DebugTrace(@ON , 'c:\trace.txt')
BoxOpen("","Press any key.")

buf1 = BinaryAlloc(256)
buf2 = BinaryAlloc(256)

before = GetKeyboardState(buf1)
after  = GetKeyboardState(buf2)
While BinaryCompare(before, 0, after, 0, 256)==@TRUE
   after = GetKeyboardState(buf2)
EndWhile
GoSub get_the_key

BinaryFree(buf1)
BinaryFree(buf2)

;==============================
Exit
;==============================


:get_the_key
key = ""
For count = 0 To 255
   x=BinaryPeek(buf2,count)
   If x<>0 && x<>1 Then Break
Next

Select count
      Case 9
         key = "Tab"
         Break
      Case 13
         key = "Enter"
         Break
      Case 16
         key = "Shift"
         Break
      Case 17
         key = "Ctrl"
         Break
      Case 18
         key = "Alt"
         Break
      Case 20
         key = "CapsLock"
         Break
      Case 27
         key = "Escape"
         Break
      Case 32
         key = "Space"
         Break
      Case 33
         key = "PgUp"
         Break
      Case 34
         key = "PgDn"
         Break
      Case 35
         key = "End"
         Break
      Case 36
         key = "Home"
         Break
      Case 37
         key = "Left arrow"
         Break
      Case 38
         key = "Up arrow"
         Break
      Case 39
         key = "Right arrow"
         Break
      Case 40
         key = "Down arrow"
         Break
      Case 45
         key = "Insert"
         Break
      Case 46
         key = "Delete"
         Break
      Case 187
         key = "="
         Break
      Case 189
         key = "-"
         Break

      Case 112
         key = "F1"
         Break
      Case 113
         key = "F2"
         Break
      Case 114
         key = "F3"
         Break
      Case 115
         key = "F4"
         Break
      Case 116
         key = "F5"
         Break
      Case 117
         key = "F6"
         Break
      Case 118
         key = "F7"
         Break
      Case 119
         key = "F8"
         Break
      Case 120
         key = "F9"
         Break
      Case 121
         key = "F10"
         Break
      Case 122
         key = "F11"
         Break
      Case 123
         key = "F12"
         Break
EndSelect

If key == "" Then key = Num2Char(count)
Message(count, key)
Return

User Reply:

The WaitForKeyEx seems to be working on our desktops so I have made a comparison (below). The full list from the problem laptops is attached. Thanks again.
Laptop (not working)		Desktop (working)

ApntEx#0|3892
Apoint#0|3716
BCMWLTRY#0|1404
			CNTAoSMgr#0|3420
			ctfmon#0|2140
			CTSVCCDA#0|1516
DataServer#0|1860
dpmw32#0|3912
DVDLauncher#0|964
hidfind#0|3884
igfxpers#0|3112
NicConfigSvc#0|2020
OfcPfwSvc#0|260
			pctsAuxs#0|1888
			pctsSvc#0|1916
			pctsTray#0|3060
QN1928#0|2472
quickset#0|4028
			RRAD27#0|2712
scardsvr#0|1600
			SnReg1S#0|1824
			TmProxy#0|872
			WindowsSearch#0|3532
			WindowsSearchFilter#0|1200
			WindowsSearchFilter#1|3968
			WindowsSearchIndexer#0|3668
WLTRAY#0|3968
WLTRYSVC#0|1392
wmiprvse#1|3800

Answer:

Do these systems have spyware tools or any other keylogging tools that might be installed?

User Reply:

No. The only real difference that I see is the antivirus but I assume that wouldn't be keylogging.

Answer:

I recommend temporarily disabling the anti-virus tool and testing WaitForKey again.

User Reply:

Removing Trend Antivirus did not effect anything.

Answer:

I was wondering if there was anything special about the keyboards. Whileas most keyboards can tell if the key is pushed down or not, some ultra small keyboards are more "electronic" and send a shortelectronic pulse for the keypress, abd cannot use used to hold a key down. Those keyboards are useless for many games that depend on holding a key down to do some actions, like move a character across a screen.

I wonder if you got that kind keyboard and it is confusing the issue.

Something unusual must be getting involved. There are no known issues with this function. At this point I believe the issue is either hardware related or some application or utility is involved.

I recommend checking for hardware updates from Dell and disabling processes one by one until the culprit is found.

User Reply:

Ok. This might take a while. When we have gone though the items I'll report back.

More:

I, too have a Dell Laptop and have recently been fighting with the same problem. I did notice another strange behavior: if you hold down a key and press another key, it does detect the second key. Maybe that will help somebody figure out what is going on...
Article ID:   W17900
Filename:   WaitForKey Issues.txt
File Created: 2008:07:03:10:32:22
Last Updated: 2008:07:03:10:32:22