locked
Simulating keyboard keys and mouse clicks outside my application RRS feed

  • Question

  • Hi,

    I would like to automate a few procedures in our company by replicating certain shortcuts on our employes desktops. Is there a way to emulate/simulate the keyboard for pressing the 'win key' and combinations like crtl+c,v,etc. using vb net?

    Thanks alot.


    The moon's orbit is artificial
    Friday, January 16, 2009 4:25 PM

Answers

  • Hi A7h0m,

    Have a look at the Sendkeys class. It should satisfy your requirement.

    About Windows keys, you need to have a look at the following code snippet with API call.

    Imports System
    Imports System.Windows.Forms

    Public Class Form1
        Private Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, _
        ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)

        Private Const KEYEVENTF_KEYUP As Integer = 2
        Private Const vbKeyControl As Integer = 17
        Private Const vbKeyEscape As Integer = 27

        Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

            ' Press the Ctrl-Esc key
            keybd_event(vbKeyControl, 0, 0, 0)
            keybd_event(vbKeyEscape, 0, 0, 0)

            ' Release the two keys
            keybd_event(vbKeyControl, 0, KEYEVENTF_KEYUP, 0)
            keybd_event(vbKeyEscape, 0, KEYEVENTF_KEYUP, 0)
        End Sub
    End Class

    Riquel

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Monday, January 19, 2009 7:04 AM
    Moderator

All replies

  • Hi A7h0m,

    Have a look at the Sendkeys class. It should satisfy your requirement.

    About Windows keys, you need to have a look at the following code snippet with API call.

    Imports System
    Imports System.Windows.Forms

    Public Class Form1
        Private Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, _
        ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)

        Private Const KEYEVENTF_KEYUP As Integer = 2
        Private Const vbKeyControl As Integer = 17
        Private Const vbKeyEscape As Integer = 27

        Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

            ' Press the Ctrl-Esc key
            keybd_event(vbKeyControl, 0, 0, 0)
            keybd_event(vbKeyEscape, 0, 0, 0)

            ' Release the two keys
            keybd_event(vbKeyControl, 0, KEYEVENTF_KEYUP, 0)
            keybd_event(vbKeyEscape, 0, KEYEVENTF_KEYUP, 0)
        End Sub
    End Class

    Riquel

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Monday, January 19, 2009 7:04 AM
    Moderator
  • vb.net can do practically anything however, I think a shortcut/icon could do it just as easily without the added code.
    Tuesday, January 20, 2009 8:29 AM