locked
keybd_event problem RRS feed

  • Question

  • Hi i am test virtual keyboard event but i have problem

    [DllImport("user32.dll", CharSet = CharSet.Auto)]
    public static extern void keybd_event(byte bVk, byte bScan, ulong dwFlags, IntPtr dwExtraInfo);

    private void Press(){

     DoKeyboardEvent(0x11);
     DoKeyboardEvent(0x50);

    }

     

    private void DoKeyboardEvent(byte buttonCode)
    {
        keybd_event(buttonCode, 0x45, KEYEVENTF_EXTENDEDKEY, IntPtr.Zero);
    }


    i am test CTRL + P   , this code is runing then  i am push my keyboard and break down  :s 

    faced with this problem before

    Saturday, June 20, 2009 11:08 PM

Answers

  • hi  i am fixed problem  :)

    example CTRL + A

     

    keybd_event(0x11, 0x9d, 0, IntPtr.Zero); 
    keybd_event(0x41, 0x9d, 0, IntPtr.Zero); 
    keybd_event(0x41, 0x9d, 0x2, IntPtr.Zero); 
    keybd_event(0x11, 0x9d, 0x2, IntPtr.Zero);
    
    

     

     


    correct
    • Marked as answer by iLyax Monday, January 18, 2010 12:48 AM
    Monday, October 26, 2009 10:26 PM

All replies

  • Do you want to know when you press CTRL + P on your keyboard? If yes, try the code below, it use getkeystate API and timer, set the timer enable property to True

     [DllImport("User32.dll")]
            private static extern bool GetAsyncKeyState(System.Int32 vKey);
            private void timer1_Tick(object sender, EventArgs e)
            {
                if (CheckInputKey() == true) //put this in timer
                {
                    MessageBox.Show("CTRL+ P is pressed");
                    this.Close();
                }
    
            }
            public bool CheckInputKey()
            {
                bool checkKey = false;
                //CTRL=17 dec value    hex value =0x11
                // P=80 dec value         ,,    ,, =0x50
                //p= 112 dec value      ,,     ,, =0x70
    //for upper p
                if (GetAsyncKeyState(17) && GetAsyncKeyState(80) 
                {
                    checkKey = true;
                }
    //for lower p
             if (GetAsyncKeyState(17) && GetAsyncKeyState(112) 
                {
                    checkKey = true;
                }
    
                return checkKey;
            }
            

    Also, you check the virtual key codes at http://msdn.microsoft.com/en-us/library/ms645540.aspx
    kaymaf

     


    I hope this helps, if that is what you want, just mark it as answer so that we can move on
    • Proposed as answer by Kenneth Haugland Thursday, June 25, 2009 7:58 PM
    • Unproposed as answer by iLyax Thursday, June 25, 2009 8:58 PM
    Saturday, June 20, 2009 11:39 PM
  • Where's your KEYEVENTF_KEYUP code?

    • Marked as answer by nobugz Tuesday, October 27, 2009 2:42 AM
    • Unmarked as answer by iLyax Monday, January 18, 2010 12:48 AM
    Sunday, June 21, 2009 3:58 AM
  • why need KEYEVENTF_KEYUP ? 

     

     

    kaymaf

    i am virtual clicked keyboard this code not have problem but then my keyboard break down 

    press ESC button then  run windows button :S

    Sunday, June 21, 2009 9:21 AM
  • You don't need it.  You should use SendKeys.  Indiscriminate use of Keybd_event is dangerous.
    • Proposed as answer by Kenneth Haugland Thursday, June 25, 2009 7:58 PM
    • Unproposed as answer by iLyax Thursday, June 25, 2009 8:58 PM
    Sunday, June 21, 2009 11:06 AM
  • Hey

    Is this what you want to achive ?

    http://www.ognedal.com/content/view/196/166/


    Kenneth
    Monday, June 22, 2009 6:15 PM
  • :S thank but need source code
    Monday, June 22, 2009 7:46 PM
  • Hi

    I work with the guy that constructed the program..  He said j that if you send him a mail and hill send it to you ;)
    Contact info:

    http://www.ognedal.com/component/option,com_contact/Itemid,3/


    I thinks it is programmed in Vb-net though...

    Kenneth
    • Marked as answer by liurong luo Friday, June 26, 2009 4:38 AM
    • Unmarked as answer by iLyax Friday, June 26, 2009 5:07 AM
    Tuesday, June 23, 2009 7:49 PM
  • hi  i am fixed problem  :)

    example CTRL + A

     

    keybd_event(0x11, 0x9d, 0, IntPtr.Zero); 
    keybd_event(0x41, 0x9d, 0, IntPtr.Zero); 
    keybd_event(0x41, 0x9d, 0x2, IntPtr.Zero); 
    keybd_event(0x11, 0x9d, 0x2, IntPtr.Zero);
    
    

     

     


    correct
    • Marked as answer by iLyax Monday, January 18, 2010 12:48 AM
    Monday, October 26, 2009 10:26 PM