www.xbdev.net
xbdev - software development
Friday March 29, 2024
home | contact | Support | Programming.. With C# and .Net...

     
 

Programming..

With C# and .Net...

 

Keyboard Input

 

 

Now at times, you'll want to have keyboard input... so I decided to put together a simple app, that demonstrates how easy it is to do.  I've stripped it down to its simplest :)  And is the starting point of a screen-mate project..heheh  As you can move the window around the screen by using the cursor keys.... If you made window into a non square window, and painted a balloon on it....you could create a balloon floating around your screen relatively easily :)

 

 

DownloadSourceCode

 

I've done a simple screen shot of what you'll expect to see when you run it up.  Now don't forget, the cursor keys move the window around... Up, Down, Left, Right.

 

 

 

So shall we take a looksy at the code and see how easy it is to respond to keyboard input?... well here is the whole code for our simple moving demo window:

 

 

Code: Keyboard.cs

// keyboard.cs

 

// C:>csc /t:winexe keyboard.cs

 

using System.Drawing; // need this for Point e.g. new Point(10,10)

 

// Well we want our program to be in a GUI window, so we need to inherit

// from Form, which is in the library 'System.Windows.Forms'.

 

class SimpleWindow : System.Windows.Forms.Form

{

      int m_xPos=100;

      int m_yPos=100;

 

      private void SimpleWindow_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)

      {

            string result = e.KeyData.ToString();

            switch (result)

            {

                  case "Left":

                        m_xPos -= 5;

                        break;

                  case "Right":

                        m_xPos += 5;

                        break;

                  case "Up":

                        m_yPos -= 5;

                        break;

                  case "Down":

                        m_yPos += 5;

                        break;

                  default:

                        break;

            }

            // Move our window based on its new coordinates

            this.Location = new Point (m_xPos,m_yPos);

      }

 

      // Window constructor

      SimpleWindow()

      {

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

      }

 

      // Our program entry point.

      static void Main()

      {

            System.Windows.Forms.Application.Run(new SimpleWindow());

      }

}

 

 

Its sweet isn't it, when you see code that you can see on a single screen, and sort of speaks for itself :)  As we create a simple...very simple window ... then in our Window Constructor, I set the function that will handle our window calls for keypresses... which in this case is "this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.SimpleWindow_KeyDown);" ... Then we go to this function, and we check for cursor key presses... and response by moving the window :)

 

 

 

 

 

 
Advert (Support Website)

 
 Visitor:
Copyright (c) 2002-2024 xbdev.net - All rights reserved.
Designated articles, tutorials and software are the property of their respective owners.