GetKeyState() for c#?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • melanieab

    GetKeyState() for c#?

    Hi,
    I need to check and see if a Tab key was pressed. I found GetKeyState() but
    that only seems to be for Visual Basic. Is there anything like it for c#?
    (GetKeyName didn't seem too promising when I looked over it.)
    Thanks!
    Melanie
  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: GetKeyState() for c#?

    Melanie,

    Why not just use the KeyDown event? It will give you a value indicating
    the key that was pressed, and any modifiers that were pressed as well (this
    includes firing if the Tab key is hit).

    Hope this helps.


    --
    - Nicholas Paldino [.NET/C# MVP]
    - mvp@spam.guard. caspershouse.co m

    "melanieab" <melanieab@disc ussions.microso ft.com> wrote in message
    news:D1DFC434-17F7-4FE8-9387-E81D0C8A86FE@mi crosoft.com...[color=blue]
    > Hi,
    > I need to check and see if a Tab key was pressed. I found GetKeyState()
    > but
    > that only seems to be for Visual Basic. Is there anything like it for c#?
    > (GetKeyName didn't seem too promising when I looked over it.)
    > Thanks!
    > Melanie[/color]


    Comment

    • wyghf

      #3
      RE: GetKeyState() for c#?

      You can use Control.Modifie rKeys. In fact,It call GetKeyState() by PInvoke.

      "melanieab" wrote:
      [color=blue]
      > Hi,
      > I need to check and see if a Tab key was pressed. I found GetKeyState() but
      > that only seems to be for Visual Basic. Is there anything like it for c#?
      > (GetKeyName didn't seem too promising when I looked over it.)
      > Thanks!
      > Melanie[/color]

      Comment

      • melanieab

        #4
        RE: GetKeyState() for c#?

        I'm not sure what's going on, but I've tried:

        if (Control.Modifi erKeys == Keys.Tab) {MessageBox.Sho w("Tab");}

        if (e.KeyCode == Keys.Tab){Messa geBox.Show("Tab ");}
        (this works for everything (up/down) but Tab)

        as well as some code that I found from someone elses question:

        "for each control that do not catch TAB key, override IsInputKey method like
        following :
        protected override bool IsInputKey(Keys keyData) {
        if (keyData==Keys. Tab) return true;
        return base.IsInputKey (keyData);
        }
        Now, KeyDown event will be called if TAB key is pressed."

        and nothing has worked, although I'm not sure I completely did the 3rd
        option correctly. (can you just put the protected override bool at the
        beginning of the program or do you somehow connect it with each textbox that
        I'd tab out of?)

        Any suggestions????

        Very frustrated,
        Mel





        "wyghf" wrote:
        [color=blue]
        > You can use Control.Modifie rKeys. In fact,It call GetKeyState() by PInvoke.
        >
        > "melanieab" wrote:
        >[color=green]
        > > Hi,
        > > I need to check and see if a Tab key was pressed. I found GetKeyState() but
        > > that only seems to be for Visual Basic. Is there anything like it for c#?
        > > (GetKeyName didn't seem too promising when I looked over it.)
        > > Thanks!
        > > Melanie[/color][/color]

        Comment

        • melanieab

          #5
          RE: GetKeyState() for c#?

          I'm not sure what's going on, but this is what I've tried:

          if (e.KeyCode == Keys.Tab){Messa geBox.Show("Tab ");}
          (this works for everything but the Tab key (up/down are ok))

          if (Control.Modifi erKeys == Keys.Tab){Messa geBox.Show("Tab ");}

          as well something I found online that answered someone elses question:

          "for each control that do not catch TAB key, override IsInputKey method like
          following :
          protected override bool IsInputKey(Keys keyData) {
          if (keyData==Keys. Tab) return true;
          return base.IsInputKey (keyData);
          }
          Now, KeyDown event will be called if TAB key is pressed."

          Although I'm not 100% sure I've done that one correctly (do you just put it
          at the beginning of the program or do you somehow specify which textboxes use
          it?)

          Any suggestions???

          Very frustrated,
          Mel


          "wyghf" wrote:
          [color=blue]
          > You can use Control.Modifie rKeys. In fact,It call GetKeyState() by PInvoke.
          >
          > "melanieab" wrote:
          >[color=green]
          > > Hi,
          > > I need to check and see if a Tab key was pressed. I found GetKeyState() but
          > > that only seems to be for Visual Basic. Is there anything like it for c#?
          > > (GetKeyName didn't seem too promising when I looked over it.)
          > > Thanks!
          > > Melanie[/color][/color]

          Comment

          Working...