210 likes | 499 Views
The Keyboard. Jim Fawcett CSE778 – Advanced Windows Programming derived from a presentation by Yi-Yang Huang & Jayashree Venkatesh. Ignoring the keyboard. Microsoft windows and windows forms class handle many keyboard functions themselves. Ignore keystrokes in menu selection.
E N D
The Keyboard Jim Fawcett CSE778 – Advanced Windows Programmingderived from a presentation by Yi-Yang Huang & Jayashree Venkatesh
Ignoring the keyboard • Microsoft windows and windows forms class handle many keyboard functions themselves. • Ignore keystrokes in menu selection. • Windows forms programs define keyboard accelerators to invoke common menu items. • Dialog boxes also have a keyboard interface, but programs need not monitor the keyboard when a dialog box is active.
Who’s got the focus? • A particular keystroke has only a single destination, which is of type Control or a descendant of Control, such as Form. • The object that has input focus receives a keystroke event. A form has input focus when it is the active form. • Active form is usually the topmost form on the desktop. • Active form is available from the only static property implemented by Form. • Type : Form Property : ActiveForm Accessibility : get
A form can attempt to activate itself by calling void Activate(). • When a form changes to or from the Activated state it gets these events: Event Method Delegate Argument --------------------------------------------------------------- Activated OnActivated EventHandler EventArgs Deactivate OnDeactivate EventHandler EventArgs
Keyboards and Characters • Keyboard is: • A collection of distinct physical keys • A means of generating character codes (Unicode). • Four groups of keys • Toggle keys (Caps, Num, Scroll locks, Insert) • Shift keys (Shift, Ctrl, Alt) • Noncharacter keys (function, pause, del) • Character keys (letter,no,Tab,Backspace,Enter,Esc)
Keys Down and Keys Up • Keystroke event handlers in your form class: Event Method Delegate Argument --------------------------------------------------------------- KeyDown OnKeyDown KeyEventHandler KeyEventArgs KeyUp OnKeyUp KeyEventHandler KeyEventArgs
Event Overrides protected override void OnKeyDown(KeyEventArgs kea) { … } protected override void OnKeyUp (KeyEventArgs kea) { …} Void MyKeyDownHandler (object objSender, KeyEventsArgs kea) { … } Void MyKeyUpHandler (object objSender, KeyEventsArgs kea) { … }
cntl.KeyDown += new KeyEventHandler (MyKeyDownHandler); cntl.KeyUp += new KeyEventHandler (MyKeyUpHandler); KeyEventArgs Properties Type Property Accessibility ------------------------------------------ Keys KeyCode get Keys Modifiers get Keys KeyData get Bool Shift get Bool Control get Bool Alt get Bool Handled get/set Int KeyValue get
Keys Enumeration • Keys has lots of members including: • Letters A – 65 , B – 66 , ….., Z – 90 • Numbers D0 – 48, D1 – 49 ….D9 – 57 • Function keys F1 – 112, F2 – 113….F24 – 135. • Keypad operators Multiply – 106, Add – 107, Subtract – 109, Divide – 111
Also in Keys Enumeration • Keypad unused:Separator • Keypad cursor movement:Home, Left, End, Insert, Up, Clear, Down, Right, Pageup / Pagedown, Right, Delete • ASCII control keys:Back, Tab, Linefeed, Enter Return, Escape, Space • Shift keys:Shift, Control , Menu, LShiftkey, Lcontrolkey, LMenu, RShiftkey, RControlkey,RMenu
Other Special Keys • Modifier keys – shift,control, alt • Eg: Shift followed by D • Masks are provided for differentiating the keycodes and modifiers. • Miscellaneous – Cancel, Pause, Capslock, Printscreen, Numlock, Scroll • Mouse buttons – LButton, RButton, Mbutton
Still More – This is Ridiculous! • Browsers and players – browserback, browserforward, browserrefresh, browserstop,…., volumemute, volumedown, volumeup,..,launchapplication1,…,mediastop,… • IME (Input method editor) – Finalmode, Kanjamode, IMEconvert,IMEaccept…. • Microsoft keys – LWin, RWin, Apps • Special keys – Select, Print, Execute, Help, Processkey, Attn, Play, Zoom • Symbols – Oemsemicolon, Oemplus, Oemcomma, Oemminus, Oemperiod, OemQuestion, Oemtilde, OemPipe, Oemquotes, Oembackslash
Testing the modifier keys • State of modifier keys can be obtained using static Control.ModifierKeys property. Keys keysmod =Control.ModifierKeysIf (keysMod == (Keys.Shift | Keys.Control)){ // Shift and Ctrl and pressed }If (keysMod == Keys.Shift) { // Shift is pressed }If (keysMod == Keys.Control)){ // Ctrl is pressed }
Key Handling • Problems with Capslock • Can’t detect current state • KeyDown is mostly used for cursor movement, Insert and Delete KeyPress for Characters Event Method Delegate Argument ----------------------------------------------------------------------------------------------------- KeyPress OnKeyPress KeyPressEventHandler KeyPressEventArgs
KeyPressEventArgs Properties Type Property Accessibility ------------------------------------------ char KeyChar get bool Handled get/set Control Characters Keyboard-Generated Control Characters Key Control Character --------------------------------------------- Shift + Ctrl @ 0x0000 Backspace 0x0008 ……. See list on page 232
Invoking the Win32 API • Platform Invocation Services ScrollWindow: Bool Scrollwindow(HWND hwnd, int Xamount, int Yamount, const RECT * lprect.CONST RECT * lpclipRECT); [StructLayout(LayoutKind,Sequential)]Typedef struct tagRECT{ Long left; Long top; Long right; Long bottom;} RECT;[DllImport(“user32.dll”)]public static extern int ScrollWindow( IntPtr hwnd, int cx, int cy, ref RECT rectScroll, ref RECT rectClip);
Handling input from foreign keyboards • Control panel – regional options – general tab – change the language settings – reboot • Input focusDetermines which control gets keyboard input. Control Properties: Type Property Accessibility --------------------------------------------- bool CanFocus get bool ContainsFocus get Bool Focused get
Control Methods Bool Focus ( ) Control Events(selection) Event Method Delegate Argument ----------------------------------------------------------------------------------- GotFocus OnGotFocus EventHandler EventArgs The Missing Caret “Cursor” is referred to in Windows as a caret. Caret caret = new Caret (form);
Caret Properties Type Property Accessibility ----------------------------------------------- Control Control get Size Size get/set Point Position get/set Bool Visibility get/set size of Caret : Caret.Size = new Size( 2, Font.height); Caret Methods : Void Hide(); Void Show(); Void Dispose();
Handling Focus protected override void OnGotFocus (EventArgs ea) { Base.OnGotFocus(ea); … } protected override void OnLostFocus (EventArgs ea) { Base.OnLostFocus(ea); … }