Vitual CTRL key - SetKeyboardState?

Posted on 2006-04-17
Last Modified: 2012-08-14
Can I use the user32 function SetKeyboardState to virtually enable the CTRL key?  I'm trying to code a button that when clicked will programatically "hold down" the CTRL key until the button is clicked again.

Any other suggestions?
Question by:CaitlinRoberts
    LVL 85

    Accepted Solution

    You can use the keybd_event() API:

        Private Declare Sub keybd_event Lib "user32.dll" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Integer, ByVal dwExtraInfo As Integer)
        Private Declare Function MapVirtualKey Lib "user32" Alias "MapVirtualKeyA" (ByVal wCode As Integer, ByVal wMapType As Integer) As Integer

        Private Sub KeyDown(ByVal key As Byte)
            keybd_event(key, MapVirtualKey(key, 0), 0, 0) ' Down
        End Sub

        Private Sub KeyUp(ByVal key As Byte)
            keybd_event(key, MapVirtualKey(key, 0), 2, 0) ' Up
        End Sub




    Author Comment


    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Join & Write a Comment

    Suggested Solutions

    Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
    Introduction When many people think of the WebBrowser ( control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    733 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now