The Mouse and Keyboard
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
? ?
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
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
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
?
Enumeration possibilities:
None, Left, Right, Middle Indicate(s) which button or buttons are currently pressed Each button corresponds to a bit set
MouseButtons MouseButtons
Sketch-dotNet
Sketching revisited
?
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)
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
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 ?
program
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
Delegate for each: EventHandler Argument for each: EventArgs ? See Mouse-Enter-Leave-Hover example program
? ?
Hide( );
The Keyboard
?
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
Form.ActiveForm static property returns the active form this.Activate() method can be used to make this form the active form
Types of Keys
?
KeyDown, KeyEventArgs
When a key is pressed (WM_KEYDOWN)
KeyPress, KeyPressEventArgs
When a character-generating key is pressed (WM_CHAR) Occurs after a KeyDown event
? ?
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
Character keys: Letters, numbers, symbol keys, spacebar, Backspace, Tab key
?
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
KeyChar Handled
True if Shift key is pressed True if Alt key is pressed Set by event handler (initially false) Returns KeyData as an integer
Handles KeyPress event ? KeyArrow: Moves an image on the forms client area in