29 April, 2011

How to Press and Hold Keyboard key Programmatically in C#, VB.NET, C/C++


                       using sendkey function we can send specified amount of keys at a time so it is not possible to press and hold any key using sendkey. therefore instead of using sendkey function i am going to use keybd_event function of user32.dll library which let us create any type of keyboard event manually. so now to use this function first we need to declare this function before using it which can be done as following.
  1. IN C#
            public static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, uint dwExtraInfo);

  2. IN VB
            <DllImport("user32.dll")> _
            Public Shared Sub keybd_event(bVk As Byte, bScan As Byte, dwFlags As UInteger,     dwExtraInfo As UInteger)
            End Sub

  3. IN C/C++
           #include <windows.h>
Once you have imported user32.dll and declared your keybd_event function now is the time to use it. before using it we must now about all the arguments of this function.

bVk [in]
Type: BYTE A virtual-key code. The code must be a value in the range 1 to 254. For a complete list, see Virtual Key Codes.
bScan [in]
Type: BYTE A hardware scan code for the key.
dwFlags [in]
Type: DWORD Controls various aspects of function operation. This parameter can be one or more of the following values.

If specified, the scan code was preceded by a prefix byte having the value 0xE0 (224).

If specified, the key is being released. If not specified, the key is being depressed.
dwExtraInfo [in]
Type: ULONG_PTR An additional value associated with the key stroke.

//these are virtual key codes for keys
const int VK_UP = 0x26; //up key
        const int VK_DOWN = 0x28;  //down key
        const int VK_LEFT = 0x25;
        const int VK_RIGHT = 0x27;
        const uint KEYEVENTF_KEYUP = 0x0002;
        const uint KEYEVENTF_EXTENDEDKEY = 0x0001;
        int press()

//Press the key
            keybd_event((byte)VK_UP, 0, KEYEVENTF_EXTENDEDKEY | 0, 0);
            return 0;
        int release()
//Release the key
            keybd_event((byte)VK_RIGHT, 0, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
            return 0;


  1. For bVk you can also use the Class "Keys" and cast it to Byte.

  2. at the same time as going with a mechanical keyboard is really an option which many gamers opt for, some either don’t have the cash for one or nonetheless choose the texture of rubber dome switches. best mechanical keyboard under 50

  3. can you give example for pressing for example (A) Key?

  4. SendKeys.Send("A");


  5. The strategy you have posted on this technology helped me to get into the next level and had lot of information in it. The angular js programming language is very popular which are most widely used

    Dot Net Training in Chennai | Dot Net Training in anna nagar | Dot Net Training in omr | Dot Net Training in porur | Dot Net Training in tambaram | Dot Net Training in velachery

  6. iot training in chennai - Iot Training in Chennai - Best IOT Training Institute in Chennai will provide the course guidance for Internet of things.

    DevOps training in chennai - Best DeVops training Institute in Chennai provide the courses on DeVops. And also help you to get placement in DevOps technology.

    blue prism training in Chennai - Best Blue prism training in Chennai providing the courses and guidance on Blue prism.

    uipath training in Chennai - Best uipath course and training in Chennai.

    microsoft azure training in chennai - Best azure training in Chennai offering the courses with guidance for Microsoft azure training.

  7. This comment has been removed by the author.

  8. This comment has been removed by the author.

  9. It’s truly incredible and extremely enlightening article . bsc 3rd year time table Thanks for sharing this extraordinary data and keep review these stunning data

  10. Incredible blog here! It's mind boggling posting with the checked and genuinely accommodating data. Skyfall Leather Jacket