jlavallet
jlavallet

Reputation: 1375

Is there a way to use the SendKeys VB function or keybd_event User32 library function to send a single Shift keystroke by itself?

I am using a voice recognition add-on to Dragon NaturallySpeaking to create keystroke automations using my voice. The add-on exposes the VB function SendKeys. I am aware that the shift key modifier (+) can be combined with almost any other character(s), but I am not trying to combine the shift key with anything; I simply want to send a single shift keypress without anything else. Is this possible?

Some things I've tried:

SendKeys "+"

SendKeys "{Shift}"

Any ideas?

UPDATE:

Based on the article posted by user14797724, it's use of the keybd_event User32 library function, and the documentation for the System.Windows.Forms.Keys enumeration, I modified the code to use left shift. Here's the code:

Imports System.Runtime.InteropServices
Imports System.Windows.Forms

Public Module SendWinKey
    Const KEYEVENTF_KEYDOWN As Integer = &H0
    Const KEYEVENTF_KEYUP As Integer = &H2

    Declare Sub keybd_event Lib "User32" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As UInteger, ByVal dwExtraInfo As UInteger)

Public Sub Main()    
        keybd_event(CByte(Keys.LShiftKey), 0, KEYEVENTF_KEYDOWN, 0) 'press the left shift key down
        keybd_event(CByte(Keys.LShiftKey), 0, KEYEVENTF_KEYUP, 0) 'release the left shift key
End Sub

End Module

I was hoping this would work for me but it appears that the scripting environment I am using does not support the Imports keyword and requires CLS-Compliant variables for an external call. I might be able to get around the Imports keyword problem by prefixing the appropriate types with their full namespace but does anybody have an alternative external call I might make that is CLS-Compliant? The UInteger type seems to be the one it doesn't like.

UPDATE 2:

I don't know what I was thinking when I put VBScript as one of my tags. I had tagged VBA but someone edited it out. As best I can tell the "language" I am using is a subset of Visual Basic. Here is a screenshot of the editor.

KnowBrainer Command Editor

Upvotes: 0

Views: 1350

Answers (2)

jlavallet
jlavallet

Reputation: 1375

So it turns out that I could substitute the Integer type for the UInteger type. Then I had to figure out how to eliminate the CByte call and the System.Windows.Forms.Keys enumeration. Lastly I just removed the unnecessary Module declaration and everything seems to be working perfectly now. And yes I did repeat the key up and key down events because I actually wanted to hit the shift key twice. Thanks to all who tried to help.

'Press and release the left shift key twice

Const LSHIFT As Byte = 160 'Defined here https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.keys?view=net-5.0
Const KEYEVENTF_KEYDOWN As Integer = &H0
Const KEYEVENTF_KEYUP As Integer = &H2

Declare Sub keybd_event Lib "User32" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Integer, ByVal dwExtraInfo As Integer)

Sub Main()    
  keybd_event(LSHIFT, 0, KEYEVENTF_KEYDOWN, 0) 'press the left shift key down
  keybd_event(LSHIFT, 0, KEYEVENTF_KEYUP, 0) 'release the left shift key
  keybd_event(LSHIFT, 0, KEYEVENTF_KEYDOWN, 0) 'press the left shift key down
  keybd_event(LSHIFT, 0, KEYEVENTF_KEYUP, 0) 'release the left shift key
End Sub

Upvotes: 2

user15832763
user15832763

Reputation:

Yeah, what he said. I can see where you put some thought into your answer. I suggest that you add comments beside the 160 to explain where that comes from.

Upvotes: 0

Related Questions