0% found this document useful (0 votes)
34 views

The Mouse and Keyboard

The document discusses the mouse and keyboard as input devices for a computer system. It provides details on mouse events like clicking, dragging, mouse movement and buttons. It also describes the mouse cursor and how it can be manipulated. For the keyboard, it discusses the different types of keys, keyboard events like key down, key press and key up. It also describes how to get information about which keys were pressed and their modifier states from the event data. Finally it provides examples of programs that handle mouse and keyboard input.

Uploaded by

kannan101
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views

The Mouse and Keyboard

The document discusses the mouse and keyboard as input devices for a computer system. It provides details on mouse events like clicking, dragging, mouse movement and buttons. It also describes the mouse cursor and how it can be manipulated. For the keyboard, it discusses the different types of keys, keyboard events like key down, key press and key up. It also describes how to get information about which keys were pressed and their modifier states from the event data. Finally it provides examples of programs that handle mouse and keyboard input.

Uploaded by

kannan101
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Mouse

The Mouse and Keyboard

? ?

A pointing device with one or more buttons Important input device, but optional ? User moves physical mouse =>
Windows moves small bitmapped image (mouse cursor) on display "Hot spot" points to a precise location on display Hot spot position constantly updated by low-level logic inside Windows

Mouse Actions
? ?

Button Down, Button Up Wheel movement ? Moving mouse ? Clicking


Pressing and releasing a mouse button ? ?

Information about Mouse


? More Forms SystemInformaton Properties: bool MousePresent int
?

Dragging
Moving mouse while a button is pressed down

MouseButtons

Double Clicking
Clicking a button twice in succession Must occur within a set period of time and with mouse cursor in approximately the same place
?

Specifies which button was pressed ? Enumeration values: None, Left, Right, Middle

bool MouseButtonsSwapped bool MouseWheelPresent int

Forms SystemInformation class has two properties that give this information:
int DoubleClickTime Size DoubleClickSize

MouseWheelScrollLines

Mouse Events
The Control Class defines 9 mouse events and 9

Some Basic Mouse Events and Handler Methods


? ?

corresponding protected event handler methods


?

Form class is derived from Control class

Only one control or form receives mouse events


The one that has its Enabled and Visible properties set to true ? If multiple controls are stacked, the enabled visible control on top receives the event ? A Form object receives mouse events only when mouse is over its client area
?

MouseDown MouseUp ? MouseMove ? MouseWheel

OnMouseDown( ) On MouseUp( ) OnMouseMove( ) On MouseWheel( )

Delegate for each event: MouseEventHandler 2 nd argument for each handler: MouseEventArgs ? ?

Click DoubleClick

OnClick( ) OnDoubleClick( )

But mouse can be captured by a control = > it can receive mouse events when mouse is not over it

Delegate for each event: EventHandler 2 nd argument for each handler: EventArgs

MouseEventArgs Properties
?

Give access to read-only data that comes with mouse event


int X Horizontal position of mouse int Y Vertical position of mouse MouseButtons Button
?

Click/DoubleClick EventArgs Static Properties


? Give access to read-only data that comes

with mouse Click and DoubleClick events


Point MousePosition
? ?

Enumeration possibilities:
None, Left, Right, Middle Indicate(s) which button or buttons are currently pressed Each button corresponds to a bit set

Result in screen coordinates To convert to client area coordinates, use PointToClient( )

int Clicks int Delta

MouseButtons MouseButtons

Sketching Example Program


? ? ?

Could use an array:


?

Saving the Sketch points


But how big?

Sketch-dotNet
Sketching revisited
?

Point[ ] apts = new Point[?????] ?

Better to use a C# dynamic ArrayList


A class defined in System.Collections namespace
?

Using C# and the .NET Framework Class Library

But if window is exposed, the sketch disappears Two ways to avoid this:
1. Save the points in each sketch and redraw all

Also has data structures like: Queue, Stack, SortedList, HashTable ArrayList arrlst = new ArrayList( );
Could hold any data type(s)

To create a new ArrayList:


?

To add elements, e.g., a Point p:


? ?

line segments in response to Paint event 2. Draw the sketch on a shadow bitmap that the program draws on while its drawing on the screen

arrlst.Add(p); Can also Insert( ) and Remove( ) elements Point p = (Point) arrlst[2]; Note typecast
Needed because indexer returns an object of type Object

Accessing an element: use an indexer as for an ordinary array


? ?

Then redraw the bitmap in response to Paint event

Number of objects in an Arraylist: arrlst.Count

New Sketch-dotNet using an ArrayList


?

A single run can have many sketches


One for each time left mouse button goes down So use one ArrayList to store the points for each sketch
?

New Sketch-dotNet using a Shadow Bitmap


? Store the window client area as a shadow

When finished (when mouse button goes up), convert to an array of Points

Use a second ArrayList to store the array of points for each sketch (i.e., an ArrayList of sketches) ?

bitmap
Draw on it and on screen when mouse moves

Each time left mouse button goes down, start a new sketchs ArrayList ? Each time mouse moves with left button down, draw line segment and add the point to current sketchs ArrayList ? In response to Paint event, use DrawLines() to draw all the line segments in each ArrayList
g.DrawLines (Pen pen, Point[ ] a_pts); // a_pts is an array of Points ?

with its left button down


Draw the shadow bitmap on the screen when a

Paint event occurs


Note that with this technique all of the

information on the original points is lost


? See the Sketch-dotNet-Bitmap example

program

See Sketch-dotNet -ArrayList example program


Here were really storing the drawing in a Metafile format

Some Other Mouse Events and Event Handlers


? ? ?

The Mouse Cursor


? ? ? ?

MouseEnter MouseLeave MouseHover

OnMouseEnter( ) OnMouseLeave( ) OnMouseHover( )

Mouse cursor has been moved onto forms client area Mouse cursor is no longer on top of client area Mouse cursor has entered client area and has stopped moving Only happens once between MouseEnter and MouseLeave events ? ?

A little bitmap on screen that indicates the location of the mouse Can change its appearance Its an object of type Cursor defined in System.Windows.Forms Get a mouse cursor from the Cursors class
Consists of 28 static read-only properties that return predefined objects

of type Cursor, e.g.:


?

Arrow, Cross, Default, Hand, Help, Ibeam, WaitCursor, etc.

Some Static read/write Properties of Cursor class:


Cursor Current Point Position For example to display the hourglass cursor on the form: ? Cursor.Current = Cursors.WaitCursor;

Delegate for each: EventHandler Argument for each: EventArgs ? See Mouse-Enter-Leave-Hover example program

? ?

Some Static Cursor methods:


Show( );

Hide( );

See MouseCursors example program

The Keyboard
?

A shared resource in Windows


All applications receive input from same keyboard But any keystroke has a single destination
?

Keys and Characters


? Think of keyboard in two ways: A collection of distinct physical keys
?

The destination is always a Control (e.g. a Form)

Object that receives a keyboard event has the input focus the active Form
? ?

Code generated by a key press or release identifies the key Code generated identifies a character in a character set
Traditionally 8-bit ASCII code In Windows, extended to 16-bit Unicode Keyboard combinations (Shift, etc.) taken into account

Usually the topmost form If form has a caption bar, it is highlighted

A means of generating character codes


?

Form.ActiveForm static property returns the active form this.Activate() method can be used to make this form the active form

Types of Keys
?

Keyboard Events & Data


? ?

Keyboard divided into four general groups of keys


Toggle keys: Pressing key changes state
?

KeyDown, KeyEventArgs
When a key is pressed (WM_KEYDOWN)

Caps Lock, Num Lock, Scroll Lock, Insert

KeyPress, KeyPressEventArgs
When a character-generating key is pressed (WM_CHAR) Occurs after a KeyDown event

Modifier keys: Pressing key affects interpretation of other keys


?

Shift, Ctrl, Alt

? ?

KeyUp, KeyEventArgs
When a key is released (WM_KEYUP)

Noncharacter keys: Not associated with displayable characters; direct a program to carry out certain actions
?

Function keys, PgUp, PgDn, Home, End, Insert, Delete, Arrow keys

Note KeyUp/KeyDown and KeyPress event data is different


KeyUp/KeyDown events provide low -level information about the keystroke which key KeyPress provides the character code
?

Character keys: Letters, numbers, symbol keys, spacebar, Backspace, Tab key
?

Generate ASCII/Unicode codes when pressed

Keyboard combinations taken care of

KeyDown/KeyUp Events
? KeyEventArgs Properties Keys KeyCode Keys Modifiers Keys KeyData
?

KeyPress Event
? When key(s) pressed correspond to character

Identifies which key Identifies shift states Combines KeyCode & Modifiers

Keys: a huge enumeration, some examples:


Keys.A, Keys.z, Keys.D0 (zero key), Keys.F1, Keys.Add, Keys.Home, Keys.Left, Keys.Back, Keys.Space, Keys.LShiftKey See Online Help on Keys enumeration

codes ? KeyPressEventArgs Properties:


char bool

KeyChar Handled

Unicode/ASCII character code Set by handler (initially false)

bool bool bool int

Shift Alt Handled KeyValue

True if Shift key is pressed True if Alt key is pressed Set by event handler (initially false) Returns KeyData as an integer

Two Example Programs


? Key: Assembles incoming characters from keyboard

into a string that is displayed on the forms client area


?

Handles Backspace key by removing last character from string

Handles KeyPress event ? KeyArrow: Moves an image on the forms client area in

response to keyboard Left/Right/Up/Down arrow key presses Handles KeyDown event

You might also like