locked
simulate tab key using pinvoke? RRS feed

  • Question

  • Hi,

    Iam devleoping an application in WinCe ,VB which has  a rich UI
    I have connected a USB key pad ,the key pad doesnt have Tab key,
    some how i need to  simulate the tab key using an existing key(* + etc..)  how to do this?

    Iam detecting the key events using KeyPress

    In Desktop application i have done the same using SendKeys.Send("{TAB}") ,In  .netCF it doesnt work.

    as far as i know other solution is using pinvoke method and use sendmessage ,
    but i dont know how to declare and call it

    could any one help to sort out this ,
    Thanks in advance,




    Friday, August 28, 2009 7:14 AM

Answers

  • Hi,

    This may help you :-
    <DllImport("coredll.dll", SetLastError := True)> _ 
    Private Shared Sub keybd_event(ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Integer, ByVal dwExtraInfo As Integer) 
    End Sub 
    Hope this helps.

    Paul Diston
    http://www.smartmobiledevice.co.uk/
    Friday, August 28, 2009 9:20 AM
  • Try this:

    const int KEYEVENTF_KEYDOWN = 0x0000;
    const int KEYEVENTF_KEYUP = 0x0002;

    [DllImport("coredll.dll")]
    static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);

    static void SendKey(Keys key)
    {
      keybd_event((byte)key, 0, KEYEVENTF_KEYDOWN, 0);
      keybd_event((byte)key, 0, KEYEVENTF_KEYUP, 0);
    }

    static void SendTabKey()
    {
      SendKey(Keys.Tab);
    }

    Monday, August 31, 2009 8:49 AM

All replies

  • Hi ,
    Thanks for the reply,
    could you please tell
    I went through the link it was helpful,

    [DllImport ("coredll.dll" , SetLastError = true )]

    static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);


    Could you please explain how to do same in Vb and call function  keybd_event
    Iam not familiar with C#,
    Thanks
    Friday, August 28, 2009 8:48 AM
  • Hi,

    This may help you :-
    <DllImport("coredll.dll", SetLastError := True)> _ 
    Private Shared Sub keybd_event(ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Integer, ByVal dwExtraInfo As Integer) 
    End Sub 
    Hope this helps.

    Paul Diston
    http://www.smartmobiledevice.co.uk/
    Friday, August 28, 2009 9:20 AM
  • Hi paul ,
    Thanks for the reply,it was really helpful,

    Imports System.Runtime.InteropServices
    Public Class Form1
    
        <DllImport("coredll.dll", SetLastError:=True)> _
           Private Shared Sub keybd_event(ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Integer, ByVal dwExtraInfo As Integer)
        End Sub
    
        Private Function callMe()
            Try
                MsgBox("callMe ")
                Dim byteTab As Byte
                Dim byteScan As Byte
                byteTab = "VK_TAB"
                byteScan = "0x09"
                //hex code for tab key
      keybd_event(byteTab, byteScan, 0, 0) Catch ex As Exception MsgBox(ex.ToString) End Try Return "" End Function Private Sub Form1_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles MyBase.KeyPress 'MsgBox("Form1_KeyPress " + e.KeyChar.ToString) If (e.KeyChar.ToString.Equals("/")) Then callMe() End If End Sub Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load End Sub End Class

    Iam getting following exception ,when i try above code

    System.invalidCastexception:MessageTextunavailable.Resource file'Microsoft.VisualBasic resorce'not found-->
    System.FormatException

    Please suggest a solution
    Thanks ,
    WinCEBeginner09












    Friday, August 28, 2009 10:02 AM
  • Try this:

    const int KEYEVENTF_KEYDOWN = 0x0000;
    const int KEYEVENTF_KEYUP = 0x0002;

    [DllImport("coredll.dll")]
    static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);

    static void SendKey(Keys key)
    {
      keybd_event((byte)key, 0, KEYEVENTF_KEYDOWN, 0);
      keybd_event((byte)key, 0, KEYEVENTF_KEYUP, 0);
    }

    static void SendTabKey()
    {
      SendKey(Keys.Tab);
    }

    Monday, August 31, 2009 8:49 AM
  • Sorry, I forgot that you needed it in VB. Here's it is in VB (not my forte, but it should give you the picture...)

    Private Const KEYEVENTF_KEYDOWN As Integer = 0
    Private Const KEYEVENTF_KEYUP As Integer = 2

    <DllImport("coredll.dll")> _
    Private Shared Sub keybd_event(ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Integer, ByVal dwExtraInfo As Integer)

    Public Shared Sub SendKey(ByVal key As Keys)
      keybd_event(CByte(key), 0, KEYEVENTF_KEYDOWN, 0)
      keybd_event(CByte(key), 0, KEYEVENTF_KEYUP, 0)
    End Sub

    Public Shared Sub SendTabKey()
      SendKey(Keys.Tab)
    End Sub

    Monday, August 31, 2009 8:54 AM