29 April, 2011

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

Introduction:-

                       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.
IMPORT DLL:-
  1. IN C#
            [DllImport("user32.dll")]
            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.
ValueMeaning

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

KEYEVENTF_KEYUP
0x0002
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.

Example:-(C#)
//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;
       }

40 comments:

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

    ReplyDelete
  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

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

    ReplyDelete
  4. SendKeys.Send("A");

    ReplyDelete

  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


    ReplyDelete
  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.


    ReplyDelete
  7. This comment has been removed by the author.

    ReplyDelete
  8. This comment has been removed by the author.

    ReplyDelete
  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

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

    ReplyDelete
  11. Annabelle loves to write and has been doing so for many years.iamlinkfeeder1 iamlinkfeeder1 iamlinkfeeder1

    ReplyDelete
  12. Great information about wilderness for beginners giving the opportunity for new people.
    Ryo Hazuki Jacket

    ReplyDelete