CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Aug 1999
    Location
    California
    Posts
    264

    Any example for keyboard pressdown event?

    How can i use the keyboard pressdown event? My application is when i press "enter" or any other key like "del""F1", i want my code to jump to do sth. How can i do that? Any example for that?

    Thanks!



    Kevin
    Best Regards,

    Kevin Shen

  2. #2
    Join Date
    Sep 2001
    Posts
    169
    private void Key_Pressed(object sender, System.Windows.Forms.KeyPressEventArgs e)
    {
    if(e.KeyChar == 13)
    {
    this.Close();
    }
    }

    above is the code which you can use ,in this i close my form in case Enter key is pressed.

  3. #3
    Join Date
    Aug 1999
    Location
    California
    Posts
    264
    what about for those function key like "del' or "F1"........... i can not find any ascii code for that......???


    Thanks!


    Kevin
    Best Regards,

    Kevin Shen

  4. #4
    Andy Tacker is offline More than "Just Another Member"
    Join Date
    Jun 2001
    Location
    55°50' N 37°39' E
    Posts
    1,503
    the solution is little bit tacky . Its for Virtual Key CAPS LOCK. you can similarly use it for any other virtual key.

    If the Control.ModifierKeys doesn't address your issue, then use Platform Invoke and call GetKeyState directly.

    Declare this class first:
    Code:
    [
    ComVisibleAttribute(false), 
    SuppressUnmanagedCodeSecurityAttribute() 
    ] 
     
    internal class NativeMethods 
    {
         [DllImport("user32.dll", CharSet=CharSet.Auto,
          ExactSpelling=true,
          CallingConvention=CallingConvention.Winapi)] 
         public static extern short GetKeyState(int keyCode); 
         public static int HIWORD(int n) 
         { 
              return ((n >> 16) & 0xffff/*=~0x0000*/); 
         }
         public static int LOWORD(int n) 
         { 
              return (n & 0xffff/*=~0x0000*/); 
         } 
    }
    Then when you want to check if Caps is down or ON, call:
    Code:
    short state = NativeMethods.GetKeyState(0x14 /*VK_CAPTIAL*/); 
    bool capsKeyDown = NativeMethods.HIWORD(state); 
    bool capsKeyON = NativeMethods.LOWORD(state);
    If you think you CAN, you can, If you think you CAN'T, you are probably right.

    Have some nice Idea to share? Write an Article Online or Email to us and You may WIN a Technical Book from CG.

  5. #5
    Join Date
    Dec 2003
    Location
    http://map.search.ch/zuerich.en.html
    Posts
    1,074
    Or alternatively catch the KeyDown event instead:

    Code:
    private void MyForm_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
    {		
        if (e.KeyCode == Keys.F1)
            Console.WriteLine("F1");
        else if (e.KeyCode == Keys.Enter)
            Console.WriteLine("Enter");
        else
            Console.WriteLine(e.KeyCode);
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured