Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. What's an easy way to add Keys programatically?

What's an easy way to add Keys programatically?

Scheduled Pinned Locked Moved C#
helptutorialquestion
8 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    That Asian Guy
    wrote on last edited by
    #1

    I am working with a component which requires a System.Windows.Forms.Keys value. Right now, the only way I know how to change this value programatically, through user input, is:

    if (input == "a")
    {
    key = Keys.A;
    }
    // ... keep going

    I am sure there is an easier way to do this. Can someone help me out? Thanks in advance.

    C A O E T 5 Replies Last reply
    0
    • T That Asian Guy

      I am working with a component which requires a System.Windows.Forms.Keys value. Right now, the only way I know how to change this value programatically, through user input, is:

      if (input == "a")
      {
      key = Keys.A;
      }
      // ... keep going

      I am sure there is an easier way to do this. Can someone help me out? Thanks in advance.

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      Is there a Keys.FromChar or something ? Is Keys an enum and Enum.Parse would do it ?

      Christian Graus Please read this if you don't understand the answer I've given you "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )

      1 Reply Last reply
      0
      • T That Asian Guy

        I am working with a component which requires a System.Windows.Forms.Keys value. Right now, the only way I know how to change this value programatically, through user input, is:

        if (input == "a")
        {
        key = Keys.A;
        }
        // ... keep going

        I am sure there is an easier way to do this. Can someone help me out? Thanks in advance.

        A Offline
        A Offline
        Anthony Mushrow
        wrote on last edited by
        #3

        Well, if your in a Console Application then you can use ReadKey which can tell you the key that was pressed.

        ConsoleKey thing = Console.ReadKey(true).Key;

        The true means that the key the user pressed will not be printed onto the screen. If you're in a Windows Application then set the forms KeyPreview property to true and use the KeyPress event which will have data on the key that was pressed.

        My current favourite word is: I'm starting to run out of fav. words!

        -SK Genius

        Game Programming articles start -here[^]-

        1 Reply Last reply
        0
        • T That Asian Guy

          I am working with a component which requires a System.Windows.Forms.Keys value. Right now, the only way I know how to change this value programatically, through user input, is:

          if (input == "a")
          {
          key = Keys.A;
          }
          // ... keep going

          I am sure there is an easier way to do this. Can someone help me out? Thanks in advance.

          O Offline
          O Offline
          OR0N
          wrote on last edited by
          #4

          You want to use the Enum.Parse static method public static object Parse( Type enumType, string value, bool ignoreCase ); Applying this method to your example goes like this: key = (Keys)Enum.Parse(typeof(Keys), input, true); Don't forget to sanitize the input value.

          1 Reply Last reply
          0
          • T That Asian Guy

            I am working with a component which requires a System.Windows.Forms.Keys value. Right now, the only way I know how to change this value programatically, through user input, is:

            if (input == "a")
            {
            key = Keys.A;
            }
            // ... keep going

            I am sure there is an easier way to do this. Can someone help me out? Thanks in advance.

            E Offline
            E Offline
            Ed Poore
            wrote on last edited by
            #5

            It doesn't always work (Unicode & foreign languages are the trickiest) but you can simply cast them:

            key = (Keys)input;

            I doubt it. If it isn't intuitive then we need to fix it. - Chris Maunder

            T 1 Reply Last reply
            0
            • E Ed Poore

              It doesn't always work (Unicode & foreign languages are the trickiest) but you can simply cast them:

              key = (Keys)input;

              I doubt it. If it isn't intuitive then we need to fix it. - Chris Maunder

              T Offline
              T Offline
              That Asian Guy
              wrote on last edited by
              #6

              I tried this and got error CS0030: Cannot convert type 'string' to 'System.Windows.Forms.Keys'

              E 1 Reply Last reply
              0
              • T That Asian Guy

                I am working with a component which requires a System.Windows.Forms.Keys value. Right now, the only way I know how to change this value programatically, through user input, is:

                if (input == "a")
                {
                key = Keys.A;
                }
                // ... keep going

                I am sure there is an easier way to do this. Can someone help me out? Thanks in advance.

                T Offline
                T Offline
                That Asian Guy
                wrote on last edited by
                #7

                Thanks, it works.

                1 Reply Last reply
                0
                • T That Asian Guy

                  I tried this and got error CS0030: Cannot convert type 'string' to 'System.Windows.Forms.Keys'

                  E Offline
                  E Offline
                  Ed Poore
                  wrote on last edited by
                  #8

                  Input has to be a char not a string.  If you know your input will be a string but only one character long then you can use:

                  Keys output = (Keys)input[0];

                  I doubt it. If it isn't intuitive then we need to fix it. - Chris Maunder

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups