Question

I tried this code:

Imports System.Windows.Forms

Public Class Form1

    Private Sub TextBox2_KeyDown(ByVal sender As System.Object, _
        ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox2.KeyDown
        If (e.KeyCode = System.Windows.Forms.Keys.Down) Then
            SendKeys("{tab}")
        End If
    End Sub
End Class

It says that "SendKeys is not declared".

I tried to add Imports System.Windows.Forms.SendKeys,but it says that this cannot be found.

When I search on the Internet, everyone is suggesting to do it exactly like this. So why doesn't it work for me? I am using .NET 3.5 Compact Framework on Windows 7 Professional, with the target platform Windows Mobile 6 Professional SDK.

Was it helpful?

Solution

Your code is incorrect, as Abbas already pointed out in his answer. You have to call the shared Send method of the SendKeys class, like so:

Imports System.Windows.Forms

Public Class Form1
    Private Sub TextBox2_KeyDown(ByVal sender As Object, ByVal e As KeyEventArgs) Handles TextBox2.KeyDown
        If (e.KeyCode = System.Windows.Forms.Keys.Down) Then
            SendKeys.Send("{tab}")
        End If
    End Sub
End Class

But that will still not solve your problem, because SendKeys is not available in the Compact Framework. If you really need this functionality (and legitimate uses are, in my experience, quite rare), then you will need to do write the code manually that does the heavy lifting.

You have a couple of options:

  1. You can P/Invoke the SendInput function, along with the required data structures, and then call this from your code. This function injects the specified input (in this case, you will want keyboard input) into the system's input stream. It is worth nothing that this is exactly that the SendKeys.Send method uses in modern versions of the framework.

    Examples of how to declare and call this function are all over the web. A simple search should turn up everything you need, no need for me to reproduce it all here. pinvoke.net is a good place to start—sometimes their more obscure declarations are erroneous, but that's unlikely with something as common as SendInput. This answer has done the copying and pasting for you already, you just need to convert it to VB.NET. Simple enough, or there are free automatic translators available.

    When using that sample code, just make sure to change the DllImport location from user32.dll (where the function is located on desktop versions of Windows) to coredll.dll (where the code is located on mobile versions).

  2. In Windows CE, you also have the PostKeybdMessage function that can be P/Invoked in a similar manner. This is probably simpler to declare and use than SendInput, but it is somewhat more limited in what it can do. It posts keyboard messages directly to the specified window, rather than adding them to the system input stream. However, in most cases, this is exactly what you want.

    <DllImport("coredll.dll", SetLastError := True)>                        _
    Shared Function PostKeybdMessage(ByVal hWnd As IntPtr,                  _
                                     ByVal vKey As UInteger,                _
                                     ByVal flags As UInteger,               _
                                     ByVal cCharacters As UInteger,         _
                                     ByVal pShiftStateBuffer As UInteger(), _
                                     ByVal pCharacterBuffer As UInteger()) As Boolean
    End Function
    
  3. You will also find suggestions various places online to P/Invoke the keybd_event function.

    Resist the temptation—as the linked official documentation indicates, it is obsolete and has been superseded by SendInput. The simplicity of keybd_event looks appealing, but it will just get you into trouble. The inability to test for errors is a major drawback. The last time anyone had to use keybd_event was on Windows 95.

OTHER TIPS

Below is some code to invoke keybd_event:

Imports System.Runtime.InteropServices

...

Public Class SystemCalls

    ' See more at http://msdn2.microsoft.com/en-us/library/ms927178.aspx
    Public Const VK_NONAME As Byte = &HFC  ' Do nothing
    Public Const VK_ESC As Byte = &H1B  ' Smartphone back-button
    Public Const VK_F4 As Byte = &H73  ' Home Screen
    Public Const VK_APP6 As Byte = &HC6  ' Lock the keys on Smartphone
    Public Const VK_F22 As Byte = &H85  ' Lock the keys on PocketPC (VK_KEYLOCK)
    Public Const VK_F16 As Byte = &H7F  ' Toggle Speakerphone
    Public Const VK_OFF As Byte = &HDF  ' Power button

    ''' <summary>
    ''' Puts `key` into to global keyboard buffer
    ''' </summary>
    ''' <param name="key"></param>
    Public Sub SendKey(ByVal key As Byte)

        Const KEYEVENTF_KEYUP As Integer = &H2
        Const KEYEVENTF_KEYDOWN As Integer = &H0
        keybd_event(key, 0, KEYEVENTF_KEYDOWN, 0)
        keybd_event(key, 0, KEYEVENTF_KEYUP, 0)

    End Sub

    <DllImport("user32.dll", CallingConvention:=CallingConvention.StdCall, _
     CharSet:=CharSet.Unicode, EntryPoint:="keybd_event", _
     ExactSpelling:=True, SetLastError:=True)>
    Private Shared Function keybd_event(ByVal bVk As Byte, ByVal bScan As Byte, _
        ByVal dwFlags As Integer, ByVal dwExtraInfo As Integer)

    End Function

Originally from here and here.

If I'm not mistaken you should use the SendKeys.Send method. Example (from MSDN):

Private Sub Form1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs)
    Handles MyBase.DoubleClick

    ' Send the enter key; since the tab stop of Button1 is 0, this 
    ' will trigger the click event.
    SendKeys.Send("{ENTER}")

End Sub
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top