0

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

jlavallet
  • 1,267
  • 1
  • 12
  • 33
  • 1
    The Shift key is a modifier, just like Ctrl and Alt. It doesn't do anything by itself - it only works when combined with another keystroke. It has no purpose by itself. It isn't actually a keystroke, it's a key state (either pressed or not pressed). What exactly would you expect a modifier key to do without something to modify? – Ken White Apr 25 '21 at 01:02
  • 1
    I have no idea why you would ask, other than the information you've provided in your question text. If you had included that information, I wouldn't have had to ask you for it, would I? Don't blame me because you failed to provide details. It doesn't change what I said anyway; SendKeys won't send keystrokes that aren't valid keystrekes, and a modifier by itself is not a valid keystroke. – Ken White Apr 25 '21 at 01:19
  • 1
    Not angry at all. They're not valid as far as SendKeys is concerned. It will not send them. As I've explained previously, Shift is a modifier key that doesn't do anything on its own except set a state (pressed or not pressed). SendKeys does not work for those types of keys alone. – Ken White Apr 25 '21 at 01:48
  • 1
    https://winsourcecode.blogspot.com/2019/06/simulates-printscreen-key-sendkeys.html?m=1 should do this. – user14797724 Apr 25 '21 at 05:26
  • Are you looking for a vbscript, VBA, VB6 or VB.Net solution? The code snippet you posted is definitely not vbscript. – Geert Bellekens Apr 26 '21 at 16:16
  • You are absolutely right. I don't know what I was thinking when I put VBScript. I had tagged VBA but someone edited it out. As best I can tell it is a subset of Visual Basic. I'll update my original post with a screenshot of the editor. – jlavallet Apr 26 '21 at 22:12

2 Answers2

2

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
jlavallet
  • 1,267
  • 1
  • 12
  • 33
0

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.