From: Steve on
Can anyone help me understand how to use the SetKeyboardState
function. I have tried several examples I found online but none of
them (nor any of my own attempts) work for sending keyboard
combinations such as Ctrl+A.

Here is the code I currently have that does not work

Private Declare Function SetKeyboardState Lib "user32" (lppbKeyState
As Byte) As Long

Private Declare Function PostMessage Lib "user32" Alias
"PostMessageA" ( _

ByVal hwnd As Long, _

ByVal wMsg As Long, _

ByVal wParam As Long, _

ByVal lParam As Long) As Long

Private Const WM_KEYDOWN As Long = &H100
Private Const KEYSTATE_KEYDOWN As Long = &H80

Sub SendCtrl_A()
Dim tmpState(0 To 255) As Byte

tmpState(vbKeyControl) = KEYSTATE_KEYDOWN
SetKeyboardState tmpState(0)

PostMessage hwnd, WM_KEYDOWN, vbKeyA, 0&

End Sub

Can someone tell me what I am doing wrong.

Thanks,
Steve
From: Jeff Johnson on
"Steve" <sredmyer(a)rfcorp.com> wrote in message
news:fb91f813-3d14-4cab-a5e3-7136f5730910(a)e5g2000yqn.googlegroups.com...

> Can anyone help me understand how to use the SetKeyboardState
> function. I have tried several examples I found online but none of
> them (nor any of my own attempts) work for sending keyboard
> combinations such as Ctrl+A.

Why not use SendInput() instead?


From: Steve on
On Jun 15, 2:07 pm, "Jeff Johnson" <i....(a)enough.spam> wrote:
> "Steve" <sredm...(a)rfcorp.com> wrote in message
>
> news:fb91f813-3d14-4cab-a5e3-7136f5730910(a)e5g2000yqn.googlegroups.com...
>
> > Can anyone help me understand how to use the SetKeyboardState
> > function.  I have tried several examples I found online but none of
> > them (nor any of my own attempts) work for sending keyboard
> > combinations such as Ctrl+A.
>
> Why not use SendInput() instead?

From my understanding, sendInput is not much better than sendkeys in
that it always sends to the currently active window. I can not be
sure the window I wish to send keystrokes to is in fact the active
window.

Thanks,
Steve
From: Karl E. Peterson on
Steve explained :
> Can anyone help me understand how to use the SetKeyboardState
> function. I have tried several examples I found online but none of
> them (nor any of my own attempts) work for sending keyboard
> combinations such as Ctrl+A.
>
> Here is the code I currently have that does not work
>
> Private Declare Function SetKeyboardState Lib "user32" (lppbKeyState
> As Byte) As Long
>
> Private Declare Function PostMessage Lib "user32" Alias
> "PostMessageA" ( _
>
> ByVal hwnd As Long, _
>
> ByVal wMsg As Long, _
>
> ByVal wParam As Long, _
>
> ByVal lParam As Long) As Long
>
> Private Const WM_KEYDOWN As Long = &H100
> Private Const KEYSTATE_KEYDOWN As Long = &H80
>
> Sub SendCtrl_A()
> Dim tmpState(0 To 255) As Byte
>
> tmpState(vbKeyControl) = KEYSTATE_KEYDOWN
> SetKeyboardState tmpState(0)
>
> PostMessage hwnd, WM_KEYDOWN, vbKeyA, 0&
>
> End Sub
>
> Can someone tell me what I am doing wrong.

Well, not precisely, because I really haven't done this much. But
there's a precise order you need to send the keystrokes in. I think
you'll want to first send the Control down, then the A down, then the A
up, then the Control up. Not sure what you're trying to do with
SetKeyboardState?

--
..NET: It's About Trust! http://vfred.mvps.org
Customer Hatred Knows No Bounds at MSFT
ClassicVB Users Regroup! comp.lang.basic.visual.misc
Free usenet access at http://www.eternal-september.org


From: Karl E. Peterson on
Steve explained :
> Can anyone help me understand how to use the SetKeyboardState
> function. I have tried several examples I found online but none of
> them (nor any of my own attempts) work for sending keyboard
> combinations such as Ctrl+A.
>
> Here is the code I currently have that does not work
>
> Private Declare Function SetKeyboardState Lib "user32" (lppbKeyState
> As Byte) As Long
>
> Private Declare Function PostMessage Lib "user32" Alias
> "PostMessageA" ( _
>
> ByVal hwnd As Long, _
>
> ByVal wMsg As Long, _
>
> ByVal wParam As Long, _
>
> ByVal lParam As Long) As Long
>
> Private Const WM_KEYDOWN As Long = &H100
> Private Const KEYSTATE_KEYDOWN As Long = &H80
>
> Sub SendCtrl_A()
> Dim tmpState(0 To 255) As Byte
>
> tmpState(vbKeyControl) = KEYSTATE_KEYDOWN
> SetKeyboardState tmpState(0)
>
> PostMessage hwnd, WM_KEYDOWN, vbKeyA, 0&
>
> End Sub
>
> Can someone tell me what I am doing wrong.

Well, not precisely, because I really haven't done this much. But
there's a precise order you need to send the keystrokes in. I think
you'll want to first send the Control down, then the A down, then the A
up, then the Control up. Not sure what you're trying to do with
SetKeyboardState?

--
..NET: It's About Trust! http://vfred.mvps.org
Customer Hatred Knows No Bounds at MSFT
ClassicVB Users Regroup! comp.lang.basic.visual.misc
Free usenet access at http://www.eternal-september.org