Extended SENDKEY for FWH ?

Extended SENDKEY for FWH ?

Postby ukoenig » Mon Mar 09, 2009 8:03 am

Hello,

Could it be possible, to do this in FWH ?

Extraordinary, Microsoft's own VB- SendKeys makes problems on some Win2k-Systems

The following source solves the problem.
The string contains the Keys to be sended.
Of cource the new SendKeysEx-Function supports also "non visible Keys",
like ALT, STRG and so on.

Code: Select all  Expand view

// the needed API-Declarations
Private Declare Sub keybd_event Lib "user32" ( _
  ByVal bVk As Byte, _
  ByVal bScan As Byte, _
  ByVal dwFlags As Long, _
  ByVal dwExtraInfo As Long)

// Visual-Basic
// -------------- 
Private Declare Function VkKeyScan Lib "User32" _
  Alias "VkKeyScanA" ( _
  ByVal cChar As Byte) As Integer

// FWH
// --------
DLL32 FUNCTION VkKeyScanA;
      ( cChar AS BYTE ) ;
   AS _INT PASCAL;
   LIB "USER32"

// Visual-Basic
// -------------- 
Private Declare Function MapVirtualKey Lib "User32" _
  Alias "MapVirtualKeyA" ( _
  ByVal wCode As Long, _
  ByVal wMapType As Long) As Long

// FWH 
// ----------
DLL32 FUNCTION MapVirKeyA;
      ( wCode AS LONG, ;
        wMapType AS LONG ) ;
   AS LONG PASCAL;
   FROM "MapVirtualKeyA" LIB "USER32"

Private Const KEYEVENTF_KEYUP = &H2
Private Const KEYEVENTF_EXTENDEDKEY = &H1
 
// Virtual KeyCodes

Private Enum eVirtualKeyCode
  VK_BAK = &H8
  VK_TAB = &H9
  VK_CLEAR = &HC
  VK_RETURN = &HD
  VK_SHIFT = &H10
  VK_CONTROL = &H11
  VK_MENU = &H12
  VK_PAUSE = &H13
  VK_CAPITAL = &H14
  VK_ESCAPE = &H1B
  VK_PRIOR = &H21
  VK_NEXT = &H22
  VK_END = &H23
  VK_HOME = &H24
  VK_LEFT = &H25
  VK_UP = &H26
  VK_RIGHT = &H27
  VK_DOWN = &H28
  VK_SELECT = &H29
  // NEW ! Windows-Key
  VK_SNAPSHOT = &H2C  
  VK_INSERT = &H2D
  VK_DELETE = &H2E
  VK_HELP = &H2F
  VK_F1 = &H70
  VK_F2 = &H71
  VK_F3 = &H72
  VK_F4 = &H73
  VK_F5 = &H74
  VK_F6 = &H75
  VK_F7 = &H76
  VK_F8 = &H77
  VK_F9 = &H78
  VK_F10 = &H79
  VK_F11 = &H7A
  VK_F12 = &H7B
  VK_F13 = &H7C
  VK_F14 = &H7D
  VK_F15 = &H7E
  VK_F16 = &H7F
  VK_NUMLOCK = &H90
  VK_SCROLL = &H91
  // New ! Windows-Key
  VK_WIN = &H5B
 // NEW ! Key for Kontextmenue
  VK_APPS = &H5D    
End Enum

// sending Text with simulating keystrokes to the active control

Public Sub SendKeysEx(ByVal sText As String)
  Dim VK As eVirtualKeyCode
  Dim sChar As String
  Dim i As Integer
  Dim bShift As Boolean
  Dim bAlt As Boolean
  Dim bCtrl As Boolean
  Dim nScan As Long
  Dim nExtended As Long
 
  // Sending each single char
  For i = 1 To Len(sText)
    // extract current char
    sChar = Mid$(sText, i, 1)
 
    // special character ?
    bShift = False: bAlt = False: bCtrl = False
    If sChar = "{" Then
      If UCase$(Mid$(sText, i + 1, 9)) = "BACKSPACE" Then
        VK = VK_BAK
        i = i + 9
      ElseIf UCase$(Mid$(sText, i + 1, 2)) = "BS" Then
        VK = VK_BAK
        i = i + 3
      ElseIf UCase$(Mid$(sText, i + 1, 4)) = "BKSP" Then
        VK = VK_BAK
        i = i + 5
      ElseIf UCase$(Mid$(sText, i + 1, 5)) = "BREAK" Then
        VK = VK_PAUSE
        i = i + 6
      ElseIf UCase$(Mid$(sText, i + 1, 8)) = "CAPSLOCK" Then
        VK = VK_CAPITAL
        i = i + 9
      ElseIf UCase$(Mid$(sText, i + 1, 6)) = "DELETE" Then
        VK = VK_DELETE
        i = i + 7
      ElseIf UCase$(Mid$(sText, i + 1, 3)) = "DEL" Then
        VK = VK_DELETE
        i = i + 4
      ElseIf UCase$(Mid$(sText, i + 1, 4)) = "DOWN" Then
        VK = VK_DOWN
        i = i + 5
      ElseIf UCase$(Mid$(sText, i + 1, 2)) = "UP" Then
        VK = VK_UP
        i = i + 3
      ElseIf UCase$(Mid$(sText, i + 1, 4)) = "LEFT" Then
        VK = VK_LEFT
        i = i + 5
      ElseIf UCase$(Mid$(sText, i + 1, 5)) = "RIGHT" Then
        VK = VK_RIGHT
        i = i + 6
      ElseIf UCase$(Mid$(sText, i + 1, 3)) = "END" Then
        VK = VK_END
        i = i + 4
      ElseIf UCase$(Mid$(sText, i + 1, 5)) = "ENTER" Then
        VK = VK_RETURN
        i = i + 6
      ElseIf UCase$(Mid$(sText, i + 1, 4)) = "HOME" Then
        VK = VK_HOME
        i = i + 5
      ElseIf UCase$(Mid$(sText, i + 1, 3)) = "ESC" Then
        VK = VK_ESCAPE
        i = i + 4
      ElseIf UCase$(Mid$(sText, i + 1, 4)) = "HELP" Then
        VK = VK_HELP
        i = i + 5
      ElseIf UCase$(Mid$(sText, i + 1, 6)) = "INSERT" Then
        VK = VK_INSERT
        i = i + 7
      ElseIf UCase$(Mid$(sText, i + 1, 3)) = "INS" Then
        VK = VK_INSERT
        i = i + 4
      ElseIf UCase$(Mid$(sText, i + 1, 7)) = "NUMLOCK" Then
        VK = VK_NUMLOCK
        i = i + 8
      ElseIf UCase$(Mid$(sText, i + 1, 4)) = "PGUP" Then
        VK = VK_PRIOR
        i = i + 5
      ElseIf UCase$(Mid$(sText, i + 1, 4)) = "PGDN" Then
        VK = VK_NEXT
        i = i + 5
      ElseIf UCase$(Mid$(sText, i + 1, 10)) = "SCROLLLOCK" Then
        VK = VK_SCROLL
        i = i + 11
      ElseIf UCase$(Mid$(sText, i + 1, 3)) = "TAB" Then
        VK = VK_TAB
        i = i + 4
      ElseIf UCase$(Mid$(sText, i + 1, 2)) = "F1" Then
        VK = VK_F1
        i = i + 3
      ElseIf UCase$(Mid$(sText, i + 1, 2)) = "F2" Then
        VK = VK_F2
        i = i + 3
      ElseIf UCase$(Mid$(sText, i + 1, 2)) = "F3" Then
        VK = VK_F3
        i = i + 3
      ElseIf UCase$(Mid$(sText, i + 1, 2)) = "F4" Then
        VK = VK_F4
        i = i + 3
      ElseIf UCase$(Mid$(sText, i + 1, 2)) = "F5" Then
        VK = VK_F5
        i = i + 3
      ElseIf UCase$(Mid$(sText, i + 1, 2)) = "F6" Then
        VK = VK_F6
        i = i + 3
      ElseIf UCase$(Mid$(sText, i + 1, 2)) = "F7" Then
        VK = VK_F7
        i = i + 3
      ElseIf UCase$(Mid$(sText, i + 1, 2)) = "F8" Then
        VK = VK_F8
        i = i + 3
      ElseIf UCase$(Mid$(sText, i + 1, 2)) = "F9" Then
        VK = VK_F9
        i = i + 3
      ElseIf UCase$(Mid$(sText, i + 1, 3)) = "F10" Then
        VK = VK_F10
        i = i + 4
      ElseIf UCase$(Mid$(sText, i + 1, 3)) = "F11" Then
        VK = VK_F11
        i = i + 4
      ElseIf UCase$(Mid$(sText, i + 1, 3)) = "F12" Then
        VK = VK_F12
        i = i + 4
      ElseIf UCase$(Mid$(sText, i + 1, 3)) = "F13" Then
        VK = VK_F13
        i = i + 4
      ElseIf UCase$(Mid$(sText, i + 1, 3)) = "F14" Then
        VK = VK_F14
        i = i + 4
      ElseIf UCase$(Mid$(sText, i + 1, 3)) = "F15" Then
        VK = VK_F15
        i = i + 4
      ElseIf UCase$(Mid$(sText, i + 1, 3)) = "F16" Then
        VK = VK_F16
        i = i + 4
 
      // NEW ! Windows-Key
      ElseIf UCase$(Mid$(sText, i + 1, 3)) = "WIN" Then
        VK = VK_WIN
        i = i + 4
 
      // NEW ! Kontextmenue
      ElseIf UCase$(Mid$(sText, i + 1, 4)) = "APPS" Then
        VK = VK_APPS
        i = i + 5
 
      // NEW ! PrintScreen (PRINT)
      ElseIf UCase$(Mid$(sText, i + 1, 5)) = "PRINT" Then
        VK = VK_SNAPSHOT
        i = i + 6
      End If
 
    ElseIf sChar = "+" Then
      // Switch-key
      VK = VK_SHIFT
 
    ElseIf sChar = "%" Then
      ' ALT
      VK = VK_MENU
 
    ElseIf sChar = "^" Then
      '
STRG
      VK = VK_CONTROL
 
    Else
      // Detect Virtual KeyCode ...
      VK = VkKeyScan(Asc(sChar))
    End If
 
    nScan = MapVirtualKey(VK, 2)
    nExtended = 0
    If nScan = 0 Then nExtended = KEYEVENTF_EXTENDEDKEY
    nScan = MapVirtualKey(VK, 0)
 
    If VK <> VK_SHIFT Then
      // Uppercase...?
      bShift = (VK And &H100)
      bCtrl = (VK And &H200)
      bAlt = (VK And &H400)
      VK = (VK And &HFF)
    End If
 
    // Press and release
    If bShift Then keybd_event VK_SHIFT, 0, 0, 0
    If bCtrl Then keybd_event VK_CONTROL, 0, 0, 0
    If bAlt Then keybd_event VK_MENU, 0, 0, 0
 
    keybd_event VK, nScan, nExtended, 0
    keybd_event VK, nScan, KEYEVENTF_KEYUP Or nExtended, 0
 
    // Shift-key release
    If bShift Then keybd_event VK_SHIFT, 0, KEYEVENTF_KEYUP, 0
    If bCtrl Then keybd_event VK_CONTROL, 0, KEYEVENTF_KEYUP, 0
    If bAlt Then keybd_event VK_MENU, 0, KEYEVENTF_KEYUP, 0
  Next i
End Sub

examples :

txtName:SetFocus()
SendKeysEx "Dieter Otter"
Extension...
In opposition to "normal" SendKeys, it is possible to press with SendKeysEx
also additional System-keys :

Windows-Key
Kontextmenue-Key
Print-Key (PrintScreen)

START > open Programms
SendKeysEx "{WIN}"

Show Kontextmenue of the TextBox
txtName:SetFocus()
SendKeys "{APPS}"

Screen of aktive Window to Clipboard
SendKeys "{PRINT}"

 


Regards
Uwe :roll:
Since 1995 ( the first release of FW 1.9 )
i work with FW.
If you have any questions about special functions, maybe i can help.
User avatar
ukoenig
 
Posts: 4043
Joined: Wed Dec 19, 2007 6:40 pm
Location: Germany

Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: Google [Bot] and 44 guests