sancarn / stdVBA

VBA Standard Library - A Collection of libraries to form a common standard layer for modern VBA applications.
MIT License
288 stars 60 forks source link

stdWindow.SendKeysEvent() - Await key received #63

Open sancarn opened 1 year ago

sancarn commented 1 year ago

Use SetWindowHookEx() to create a KeyboardHook to ensure all keys sent by keybd_event() are received.

Pseudo-code:

Function KeybdHook(key, ...)
  For iKey = 1 to len(keys)
    if not keys(iKey).received then
      if key.code = keys(iKey).code then
        key.received = true
        exit for
      end if
      exit for
    end if
  next
  KeybdHook = CallNextHookEx(...)
End Function

Function SendKeysEvent(..., Optional bWaitForRetrieval as boolean)
  if bWaitForRetrieval then iHook = SetWindowHookEx(ActiveWnd, WH_KEYBOARD, AddressOf KeybdHook)
  For iKey = 1 to len(keys)
    keybd_event(keys(iKey))
  next 
  if bWaitForRetrieval then
    While not keys(len(keys)).received
      DoEvents
    Wend
    Call ClearWindowHookEx(iHook)
  end if

End Function

See also: