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

Sending Keystrokes

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

SendKey for Special Key Stroke


Question:

I would like to do a sendkey/sendkeyto to the desktop for Fn with F5 (to turn on bluetooth). What is the sendkey code for Fn?

I need the code for the 'Fn' key on our IBM laptops. It is located in the bottom left corner of laptop keyboards and must be held while pressing the F5 key to toggle the bluetooth power.

Answer:

; The trick is figuring out the scan code for the key when the Fn key is down.
; On my Dell the Fn+Dn keys mutes the speakers.  The scan code for this is 32
; You may find the scan codes for your notebook via Google or by trial and error.

#DefineFunction SendFnKey( wScanCode )

   ; Initialize
   nSize    = 28  ; Size of input structure.
   nOffset  = 0   ; Current buffer location.

   ; Flag member values
   INPUT_KEYBOARD        = 1
   KEYEVENTF_EXTENDEDKEY = 1  ; Causes a 0x e0 to be sent ahead of the key
                              ; This gives the scan code the the "fn" meanding.
   KEYEVENTF_KEYUP       = 2
   KEYEVENTF_SCANCODE    = 8

   ; 2 stucture buffer.
   Input  = BinaryAlloc(nSize * 2 )

   ; Key down structure
   BinaryPoke4(Input, nOffset, INPUT_KEYBOARD) ; type
   nOffset = nOffset + 4
   BinaryPoke2(Input, nOffset, 0)  ; wVk  - ignored
   nOffset = nOffset + 2
   BinaryPoke2(Input, nOffset, wScanCode)  ; wScan
   nOffset = nOffset + 2
   nFlagOff = nOffset
   BinaryPoke4(Input, nOffset, KEYEVENTF_EXTENDEDKEY|KEYEVENTF_SCANCODE)  ; dwFlags
   nOffset = nOffset + 4
   BinaryPoke4(Input, nOffset, 0)  ; time - not used
   nOffset = nOffset + 4
   BinaryPoke4(Input, nOffset, 0)  ; dwExtraInfo - not used

   ; Key up structure
   nOffset = nSize
   BinaryPoke4(Input, nOffset, INPUT_KEYBOARD) ; type
   nOffset = nOffset + 4
   BinaryPoke2(Input, nOffset, 0)  ; wVk  - ignored
   nOffset = nOffset + 2
   BinaryPoke2(Input, nOffset, wScanCode)  ; wScan
   nOffset = nOffset + 2
   nFlagOff = nOffset
   BinaryPoke4(Input, nOffset, KEYEVENTF_EXTENDEDKEY|KEYEVENTF_SCANCODE|KEYEVENTF_KEYUP)  ; dwFlags
   nOffset = nOffset + 4
   BinaryPoke4(Input, nOffset, 0)  ; time - not used
   nOffset = nOffset + 4
   BinaryPoke4(Input, nOffset, 0)  ; dwExtraInfo - not used

   ; Get a structure pointer.
   pInput = IntControl(42, Input, 0, 0, 0)

   ; Do the deed.
   sDll = StrCat(DirWindows(1), "user32.dll")
   nResult = DllCall(sDll, long:"SendInput", long:2, long:pInput, long:nSize ); key down

   ; Clean up
   BinaryFree(Input)

   Return nResult ; 0 on failure otherwise number of inputs.
#EndFunction


; Mute the speakers.
nResult = SendFnKey(32)

If nResult == 2
   sText = "Success"
Else
   sText = "Failure"
EndIf
Message("SendFnKey", sText)

Article ID:   W17261
File Created: 2007:07:03:14:29:04
Last Updated: 2007:07:03:14:29:04