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. Problems with Keyboard input (KeyDown, KeyPress, KeyUp)

Problems with Keyboard input (KeyDown, KeyPress, KeyUp)

Scheduled Pinned Locked Moved C#
graphicscsharptestingbeta-testinghelp
6 Posts 2 Posters 2 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.
  • P Offline
    P Offline
    poppabaggins
    wrote on last edited by
    #1

    Hi, I'm a rather new C# user and I'm still figuring out the ropes of forms programming and such. For the part of the program I'm working on, I basically want to move an image around on the screen, in a region (advanced, I know ). I achieved this just fine with mouse click and drag, but I'm unable to get any sort of keyboard input. Here's the part of the code I'm using:

        protected override void OnPaint(PaintEventArgs e)
        {
            Graphics g = e.Graphics;
    
            using(Region boardRegion = new Region(new Rectangle(ClientRectangle.X, ClientRectangle.Y,
                (int)(ClientRectangle.Width), (int)(ClientRectangle.Height \* boardRegionSize))))
            {
                g.Clip = boardRegion;
                using(Bitmap boardImage = new Bitmap(board.ImagePath)){
                    g.DrawImage(boardImage, board.X, board.Y);
                }
            }
            base.OnPaint(e);
        }
    
        private void CMGame\_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Up)
                board.Y -= 30;
        }
    

    And for part of InitializeComponent(), I have

    this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.CMGame_KeyDown);

    (automatically done by windows) Basically, nothing happens when I press a key (the up arrow or anything else, since I've tried different keys in testing) I read somewhere that I could try overriding WndProc, but I don't know how I would do this (even after I wrote the override method header and and found the code for KeyDown, 0x0100) For reference, I also have a button in the form with a click event. Help would be much appreciated.

    D 1 Reply Last reply
    0
    • P poppabaggins

      Hi, I'm a rather new C# user and I'm still figuring out the ropes of forms programming and such. For the part of the program I'm working on, I basically want to move an image around on the screen, in a region (advanced, I know ). I achieved this just fine with mouse click and drag, but I'm unable to get any sort of keyboard input. Here's the part of the code I'm using:

          protected override void OnPaint(PaintEventArgs e)
          {
              Graphics g = e.Graphics;
      
              using(Region boardRegion = new Region(new Rectangle(ClientRectangle.X, ClientRectangle.Y,
                  (int)(ClientRectangle.Width), (int)(ClientRectangle.Height \* boardRegionSize))))
              {
                  g.Clip = boardRegion;
                  using(Bitmap boardImage = new Bitmap(board.ImagePath)){
                      g.DrawImage(boardImage, board.X, board.Y);
                  }
              }
              base.OnPaint(e);
          }
      
          private void CMGame\_KeyDown(object sender, KeyEventArgs e)
          {
              if (e.KeyCode == Keys.Up)
                  board.Y -= 30;
          }
      

      And for part of InitializeComponent(), I have

      this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.CMGame_KeyDown);

      (automatically done by windows) Basically, nothing happens when I press a key (the up arrow or anything else, since I've tried different keys in testing) I read somewhere that I could try overriding WndProc, but I don't know how I would do this (even after I wrote the override method header and and found the code for KeyDown, 0x0100) For reference, I also have a button in the form with a click event. Help would be much appreciated.

      D Offline
      D Offline
      DaveyM69
      wrote on last edited by
      #2

      You need to tell the control that's holding the image that is needs to be redrawn either by calling it's invalidated method or calling onpaint directly depending on the situation. You could hook this up to be done automatically by putting it in a locationchanged event which you can fire whenever X or Y is changed

      Dave
      BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
      Expect everything to be hard and then enjoy the things that come easy. (code-frog)

      P 1 Reply Last reply
      0
      • D DaveyM69

        You need to tell the control that's holding the image that is needs to be redrawn either by calling it's invalidated method or calling onpaint directly depending on the situation. You could hook this up to be done automatically by putting it in a locationchanged event which you can fire whenever X or Y is changed

        Dave
        BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
        Expect everything to be hard and then enjoy the things that come easy. (code-frog)

        P Offline
        P Offline
        poppabaggins
        wrote on last edited by
        #3

        Right, I actually had that earlier before I started messing with things. Even with this.Invalidate() after it, nothing happens. I think that the event isn't being fired at all, since, for parts of my test, I just told the keydown, keypress, and keyup to change the text of the form. Nothing happened anytime I pressed a button.

        P 1 Reply Last reply
        0
        • P poppabaggins

          Right, I actually had that earlier before I started messing with things. Even with this.Invalidate() after it, nothing happens. I think that the event isn't being fired at all, since, for parts of my test, I just told the keydown, keypress, and keyup to change the text of the form. Nothing happened anytime I pressed a button.

          P Offline
          P Offline
          poppabaggins
          wrote on last edited by
          #4

          Ok, I found my problem. Once I removed my button, the key input worked again. Can anyone tell me what else would have worked?

          modified on Sunday, August 17, 2008 7:36 PM

          D 1 Reply Last reply
          0
          • P poppabaggins

            Ok, I found my problem. Once I removed my button, the key input worked again. Can anyone tell me what else would have worked?

            modified on Sunday, August 17, 2008 7:36 PM

            D Offline
            D Offline
            DaveyM69
            wrote on last edited by
            #5

            Is it a focus problem? Was the button getting the keyboard input? If so, setting the form's KeyPreview property to true might be the solution.

            Dave
            BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
            Expect everything to be hard and then enjoy the things that come easy. (code-frog)

            P 1 Reply Last reply
            0
            • D DaveyM69

              Is it a focus problem? Was the button getting the keyboard input? If so, setting the form's KeyPreview property to true might be the solution.

              Dave
              BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
              Expect everything to be hard and then enjoy the things that come easy. (code-frog)

              P Offline
              P Offline
              poppabaggins
              wrote on last edited by
              #6

              Thanks, setting the KeyPreview to true worked perfectly for me.

              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