1 / 21

The Keyboard

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.

omer
Download Presentation

The Keyboard

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. The Keyboard Jim Fawcett CSE778 – Advanced Windows Programmingderived from a presentation by Yi-Yang Huang & Jayashree Venkatesh

  2. 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.

  3. 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

  4. 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

  5. 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)

  6. Keys Down and Keys Up • Keystroke event handlers in your form class: Event Method Delegate Argument --------------------------------------------------------------- KeyDown OnKeyDown KeyEventHandler KeyEventArgs KeyUp OnKeyUp KeyEventHandler KeyEventArgs

  7. 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) { … }

  8. 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

  9. 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

  10. 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

  11. 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

  12. 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

  13. 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 }

  14. 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

  15. 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

  16. 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);

  17. 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

  18. 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);

  19. 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();

  20. Handling Focus protected override void OnGotFocus (EventArgs ea) { Base.OnGotFocus(ea); … } protected override void OnLostFocus (EventArgs ea) { Base.OnLostFocus(ea); … }

  21. End of Presentation

More Related