Upload
others
View
5
Download
0
Embed Size (px)
Citation preview
User Interfaces
Lecture 19
Cocoa: Mouse and Keyboard Events
Hamza [email protected]
September 18, 2018
Last Lecture
Where did we stop?
1
Hamza Bennani -*- COSC346 -*- Cocoa: Mouse and Keyboard Events -*- September 18, 2018
Events
I Events get filtered into aqueue by MacOS X
I Some events never reachthe application, like ?, or ?
I The active applicationprocesses events one at atime from the queue
2
Hamza Bennani -*- COSC346 -*- Cocoa: Mouse and Keyboard Events -*- September 18, 2018
The event loop
I Events are encoded as NSEvent objects and put into aqueue
I The active application takes events from the queue andprocesses them
I NSEvent contains information such as location, time, etc . . .
3
Hamza Bennani -*- COSC346 -*- Cocoa: Mouse and Keyboard Events -*- September 18, 2018
Where events go?
I The active application NSApp object issues a sendEvent:message to the active window
I A mouse event is forwarded to the view where the mouse ispointing
I A key event is forwarded to the window’s “responder chain"
4
Hamza Bennani -*- COSC346 -*- Cocoa: Mouse and Keyboard Events -*- September 18, 2018
NSEvent
I Events are passed to handling methods as NSEventobjects
I For mouse events, information includes:I locationInWindowI modifierFlags (NSShiftKeyMask, NSControlKeyMask, . . . )I timestampI window - window where event occurredI clickCount - number of mouse clicksI pressure - tabletI deltaX, deltaY - change in position
I For key events, information includes:I characters - an NSString object with typed keysI isARepeat -YES or NO regarding whether key is held downI keyCode - actual key that user pressedI modifierFlags - same as mouse modifierFlags
5
Hamza Bennani -*- COSC346 -*- Cocoa: Mouse and Keyboard Events -*- September 18, 2018
NSResponder
I . . . is an abstract class that dispatches received NSEventsto methods corresponding to various mouse and keyboardevents
I All event-handling methods are declared in NSResponder.The following classes inherit from it:
I NSWindow - so all windowsI NSView - so almost everything that’s in the window,
including NSControl objects, such as buttons, etc.I NSApplicationI . . . and even controllers: NSWindowController,
NSViewControllerI You can override NSResponder messages that you want to
handle in your custom view
6
Hamza Bennani -*- COSC346 -*- Cocoa: Mouse and Keyboard Events -*- September 18, 2018
NSResponder Hierarchy
7
Hamza Bennani -*- COSC346 -*- Cocoa: Mouse and Keyboard Events -*- September 18, 2018
Responding to Mouse Events
I Methods: mouseDown, mouseUp, mouseDragged,mouseEntered, mouseExited, mouseMoved, scrollWheel
I Note: certain rules apply to mouse events, e.g., mouse-upmust be preceded by mouse-down, mouse-dragged mustoccur between mouse-down and mouse-up, etc.
I The methods get passed an argument of NSEvent type,which responds to methods that provide information aboutthe mouse event:
I clickCount - number of clicks that occurredI windowLocation - location where mouse was clickedI Methods to obtain scroll wheel information
8
Hamza Bennani -*- COSC346 -*- Cocoa: Mouse and Keyboard Events -*- September 18, 2018
Responding to Key Events
I Key events are morecomplicated than mouseevents, because they lacka target location
I Certain key combinationsgo straight to menu bar(e.g., command-Z), othersto operating system (e.g.,Mission Control)
9
Hamza Bennani -*- COSC346 -*- Cocoa: Mouse and Keyboard Events -*- September 18, 2018
The Responder Chain
I Key events are processed usingthe responder chain
I A message is initially passed tothe first responder
I If the first responder does notaccept the message, it ispassed to the nextResponder
I Responder chain implements thechain of responsibility designpattern
I The responder chain isimplemented in NSResponder
10
Hamza Bennani -*- COSC346 -*- Cocoa: Mouse and Keyboard Events -*- September 18, 2018
The Responder Chain
11
Hamza Bennani -*- COSC346 -*- Cocoa: Mouse and Keyboard Events -*- September 18, 2018
Window Types
I Assumption: in any application, even if it is multi-window,there is only one window with the user’s focus. Therefore,the windows are categorised into the following types:
I Main window - the window that the user is working incurrently
I Key window - the window that accepts user’s inputI Inactive window - all other windowsI The main and key windows often are the same window -
common exception are window panels for opening, saving,preferences, etc., which take input focus, but are not mainwindows
I The NSApplication singleton has separate references to themain and key windows (although often these refer to thesame window object)
12
Hamza Bennani -*- COSC346 -*- Cocoa: Mouse and Keyboard Events -*- September 18, 2018
First ResponderWindow Change
I Each window has a first responder outletI The active window’s first responder gets key events
I The inner workings of NSApplication update its keyWindowoutlet to correspond to the window currently selected bythe user
I A mouse click on an inactive window does not send anevent to that window, it just changes that window to becomethe keyWindow
13
Hamza Bennani -*- COSC346 -*- Cocoa: Mouse and Keyboard Events -*- September 18, 2018
First ResponderWindow Change
14
Hamza Bennani -*- COSC346 -*- Cocoa: Mouse and Keyboard Events -*- September 18, 2018
Initial First Responder
I What does the first responder outlet point to just after awindow is created?
I Initial first responder is the element that window’s firstresponder points to by default
I You can set the initial first responder:
15
Hamza Bennani -*- COSC346 -*- Cocoa: Mouse and Keyboard Events -*- September 18, 2018
First ResponderView Change
I The inner workings of theNSWindow object updatesits firstResponder outlet toreference the currentlyselected view
I The first responder for theapplication is the objectpointed to by thefirstResponder referenceof the key Window
16
Hamza Bennani -*- COSC346 -*- Cocoa: Mouse and Keyboard Events -*- September 18, 2018
First Responder Placeholder
I In Interface Builder there’s a placeholder object that standsin for an arbitrary first responder
I You can set custom action methods for this placeholderI You can connect target actions from window elements to
the first responder placeholderI At run-time, when the window element is selected, it sends
an action to the object that happens to be the current firstresponder
17
Hamza Bennani -*- COSC346 -*- Cocoa: Mouse and Keyboard Events -*- September 18, 2018
NSControl
I Object that inherits from NSResponderI It contains an NSCell object, which has a reference to
target object and action selectorI Buttons, check boxes, and many other UI controls inherit
from NSControlI Each different control overrides selected NSResponder
methods to redirect the event to the action selector of thetarget object (if target and action have been set)
I Example: NSButton class overrides mouseDown eventinvoking the action method of its corresponding target
I Target and action values can be set through InterfaceBuilder
I We have seen how to do this in Lecture 15/16
18
Hamza Bennani -*- COSC346 -*- Cocoa: Mouse and Keyboard Events -*- September 18, 2018
NSControl
I Target and action values can also be set programmaticallyI This example modifies the ViewController of a storyboardI myB is an NSButton without an IBAction set in the Xcode
IB
19
Hamza Bennani -*- COSC346 -*- Cocoa: Mouse and Keyboard Events -*- September 18, 2018
NSControl Hierarchy
20
Hamza Bennani -*- COSC346 -*- Cocoa: Mouse and Keyboard Events -*- September 18, 2018
Things To Watch out for?!
I The pre-made objects in the Cocoa’s object library thatinherit from NSResponder override various methods toimplement specific behaviour for the type of interface theyrepresent
I Example1:I NSButton (which is an NSControl), overrides mouseDown:
method to execute an action of the corresponding targetI If you extend NSButton and override mouseDown: method,
you may disable the target action functionalityI Example2:
I NSView becomeFirstResponder: returns NO by defaultI Override method in your custom view to return YES if you
want the view to become a part of the responder chain
21
Hamza Bennani -*- COSC346 -*- Cocoa: Mouse and Keyboard Events -*- September 18, 2018
SummaryWe examined how Cocoa applications process events and introduced someconcepts that make dealing with events in your program easier
I NSEvent - object representing an event with all informationabout it
I NSResponder - an abstract class that sorts out the eventsit receives into more readable methods corresponding tomouse and keyboard events
I NSControl - an abstract class that contains a reference toa target and action, which can be triggered after an event
I First responder - reference to an object in a window thathas focus
I First responder placeholder - object in Interface Builderwith custom actions to which controls can be connected: atruntime, whenever the corresponding control event triggersan action, it gets sent to the target that happens to be thecurrent first responder
22
Hamza Bennani -*- COSC346 -*- Cocoa: Mouse and Keyboard Events -*- September 18, 2018
Timer AppMulti Window
23
Hamza Bennani -*- COSC346 -*- Cocoa: Mouse and Keyboard Events -*- September 18, 2018
Timer AppFirst Responder
24
Hamza Bennani -*- COSC346 -*- Cocoa: Mouse and Keyboard Events -*- September 18, 2018