by kirupa |
7 March
2007
In the
previous page you downloaded the partial source file and
received a quick tour of what the code does. In this page
let's write some code and create something useful using
keyboard input.
Let's start by detecting which key was pressed. For example,
let's say that every time the letter K is pressed, we play a
default system sound. The code for doing that would be:
- private
void
KeyDownEventHanlder(object
sender, KeyEventArgs e)
- {
- if
(e.Key
==
Key.K)
- {
- System.Media.SystemSounds.Exclamation.Play();
- }
- }
If you were to add the above non-grayed out lines to your
KeyDownEventHandler method
and run the program, everything still looks the same. When
you start typing in your textbox, and more importantly, when
you hit the letter K on your keyboard, the default Windows
exclamation sound will play. All of your other keys simply
display their letters on the screen, but you single out the
letter K to also play a sound when pressed.
Let's look in detail at how the key detection works.
First, I access my event argument e's
Key property:
- private
void
KeyDownEventHanlder(object
sender, KeyEventArgs e)
- {
- if
(e.Key
==
Key.K)
- {
- System.Media.SystemSounds.Exclamation.Play();
- }
- }
The Key property keeps track of the key currently being
pressed because your KeyDownEventHandler runs every time you
press down on a Key. I place
e.Key as part of a condition
for my if statement because I want to check whether the key
currently pressed matches the key that I want pressed, which
is K:
- private
void
KeyDownEventHanlder(object
sender, KeyEventArgs e)
- {
- if
(e.Key
==
Key.K)
- {
- System.Media.SystemSounds.Exclamation.Play();
- }
- }
The check whether our e.Key
value is equal to the letter K which is represented by
Key.K. When the letter pressed matches the letter we want, the if
statement becomes true and whatever we wish to execute....well,
executes!
This is great for individual letters, but what if you
want to provide some sort of Save functionality where your
users have to press two keys such as Ctrl + S? The above
approach won't work too well, solet's look at
how to do that in the next section.
The information from the following
section is almost entirely based on my blog post on this
topic:
http://blog.kirupa.com/?p=68
Key combinations are a fancy pair of words to describe
pressing/holding multiple keyboard buttons to perform a
command. For example, if you have ever used Ctrl + S
to Save, Ctrl + C, to Copy, or Alt
+ F4 to close an application, then you have used
key combinations. There are many such combinations, and
while I provided some common ones, many applications ranging
from Flash to Visual Studio provide their own key
combinations to help save you some time. Let's add some key
combinations to our little program also.
To add key combinations, modify/overwrite your
KeyUpEventHandler with the
following code:
- private
void
KeyUpEventHanlder(object
sender,
KeyEventArgs
e)
- {
- // Ctrl + S
- if
((Keyboard.Modifiers
==
ModifierKeys.Control)
&&
(e.Key
==
Key.S))
- {
- MessageBox.Show("Save!");
- }
-
- // Ctrl + N
- if
((Keyboard.Modifiers
==
ModifierKeys.Control)
&&
(e.Key
==
Key.N))
- {
- MessageBox.Show("New!");
- }
-
- // Ctrl + O
- if
((Keyboard.Modifiers
==
ModifierKeys.Control)
&&
(e.Key
==
Key.O))
- {
- MessageBox.Show("Open!");
- }
- }
If you run your application again and give your textbox
focus (i.e. clicking inside it), pressing either Ctrl +
S, Ctrl + N, or Ctrl + O will display a corresponding dialog
box. On the
next page,
let's look at the interesting parts of the above code that
cause it to work in greater detail!
Onwards to the
next page!
|