Event Binding. Different Approaches Global Hooks. 2.5 Event Binding 1

Similar documents
Events. Dispatch, event-to-code binding. Review: Events Defined 1/17/2014. occurrence.

Event Dispatch. Review: Events

The AWT Event Model 9

Event Driven Programming

GUI Event Handling 11. GUI Event Handling. Objectives. What is an Event? Hierarchical Model (JDK1.0) Delegation Model (JDK1.1)

Control Flow: Overview CSE3461. An Example of Sequential Control. Control Flow: Revisited. Control Flow Paradigms: Reacting to the User

GUI DYNAMICS Lecture July 26 CS2110 Summer 2011

GUI Program Organization. Sequential vs. Event-driven Programming. Sequential Programming. Outline

Example Programs. COSC 3461 User Interfaces. GUI Program Organization. Outline. DemoHelloWorld.java DemoHelloWorld2.java DemoSwing.

Windows and Events. created originally by Brian Bailey

Java for Interfaces and Networks (DT3010, HT10)

Previously, we have seen GUI components, their relationships, containers, layout managers. Now we will see how to paint graphics on GUI components

CSE 331 Software Design & Implementation

Graphical User Interfaces 2

G51PGP Programming Paradigms. Lecture 008 Inner classes, anonymous classes, Swing worker thread

Lecture 19 GUI Events

AP Computer Science Unit 13. Still More Graphics and Animation.

Introduction to GUIs. Principles of Software Construction: Objects, Design, and Concurrency. Jonathan Aldrich and Charlie Garrod Fall 2014

Virtualians.ning.pk. 2 - Java program code is compiled into form called 1. Machine code 2. native Code 3. Byte Code (From Lectuer # 2) 4.

Graphical User Interface (GUI)

Dr. Hikmat A. M. AbdelJaber

Graphical User Interface (GUI)

Lecture 28. Exceptions and Inner Classes. Goals. We are going to talk in more detail about two advanced Java features:

Lecture 3: Java Graphics & Events

Chapter 1 GUI Applications

Programming Languages and Techniques (CIS120)

H212 Introduction to Software Systems Honors

Lecture 5: Java Graphics

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120)

11/7/12. Discussion of Roulette Assignment. Objectives. Compiler s Names of Classes. GUI Review. Window Events

Class 16: The Swing Event Model

CSE 331 Software Design and Implementation. Lecture 19 GUI Events

GUI Event Handlers (Part I)

Systems Programming Graphical User Interfaces

Event Handling Java 7

CS11 Java. Fall Lecture 4

BM214E Object Oriented Programming Lecture 13

Advanced Java Programming (17625) Event Handling. 20 Marks

Which of the following syntax used to attach an input stream to console?

Programming Languages and Techniques (CIS120)

Graphical User Interfaces 2

Method Of Key Event Key Listener must implement three methods, keypressed(), keyreleased() & keytyped(). 1) keypressed() : will run whenever a key is

CS11 Java. Fall Lecture 3

Graphical User Interfaces 2

Come & Join Us at VUSTUDENTS.net

Java Graphical User Interfaces AWT (Abstract Window Toolkit) & Swing

Graphic User Interfaces. - GUI concepts - Swing - AWT

CSE 8B Intro to CS: Java

public void mouseexited (MouseEvent e) setminute(getminute()+increment); 11.2 public void mouseclicked (MouseEvent e) int x = e.getx(), y = e.gety();

Unit 7: Event driven programming

COMPSCI 230. Software Design and Construction. Swing

Packages: Putting Classes Together

HW#1: Pencil Me In Status!? How was Homework #1? Reminder: Handouts. Homework #2: Java Draw Demo. 3 Handout for today! Lecture-Homework mapping.

Class, Variable, Constructor, Object, Method Questions

Inheritance. One class inherits from another if it describes a specialized subset of objects Terminology:

CS 251 Intermediate Programming GUIs: Event Listeners

GUI Software Architecture

UNIT-3 : MULTI THREADED PROGRAMMING, EVENT HANDLING. A Multithreaded program contains two or more parts that can run concurrently.

Java & Graphical User Interface II. Wang Yang wyang AT njnet.edu.cn

Introduction to concurrency and GUIs

Events. Events and the Event Loop Animation Double Buffering. 1.5 Events 1

CS 106A, Lecture 14 Events and Instance Variables

The JFrame Class Frame Windows GRAPHICAL USER INTERFACES. Five steps to displaying a frame: 1) Construct an object of the JFrame class

Graphical Interfaces

Windows Programming. 1 st Week, 2011

PROGRAMMING DESIGN USING JAVA (ITT 303) Unit 7

Jonathan Aldrich Charlie Garrod

Java: Graphical User Interfaces (GUI)

Görünt Görün ülerin ölçeklenmesi ve yerle y ştirilmesi ş

PIC 20A Anonymous classes, Lambda Expressions, and Functional Programming

CSSE 220. Event Based Programming. Check out EventBasedProgramming from SVN

Programming Language Concepts: Lecture 8

Swing from A to Z Some Simple Components. Preface

Handling Mouse and Keyboard Events

CS 106A, Lecture 14 Events and Instance Variables

CSIS 10A Assignment 14 SOLUTIONS

(listener)... MouseListener, ActionLister. (adapter)... MouseAdapter, ActionAdapter. java.awt AWT Abstract Window Toolkit GUI

Building custom components IAT351

CSC207 Week 4. Larry Zhang

Heavyweight with platform-specific widgets. AWT applications were limited to commonfunctionality that existed on all platforms.

G51PRG: Introduction to Programming Second semester Applets and graphics

CS506 Web Programming and Development Solved Subjective Questions With Reference For Final Term Lecture No 1

Outline. Topic 9: Swing. GUIs Up to now: line-by-line programs: computer displays text user types text AWT. A. Basics

GUI in Java TalentHome Solutions

Java Programming. Events and Listeners

DCS235 Software Engineering Exercise Sheet 2: Introducing GUI Programming

Java Programming Lecture 7

DHANALAKSHMI SRINIVASAN COLLEGE OF ENGINEERING AND TECHNOLOGY ACADEMIC YEAR (ODD SEM)

Event Dispatch. Interactor Tree Lightweight vs. Heavyweight Positional Dispatch Focus Dispatch. 2.4 Event Dispatch 1

Class 27: Nested Classes and an Introduction to Trees

Programming in graphical environment. Introduction

Java Mouse Actions. C&G criteria: 5.2.1, 5.4.1, 5.4.2,

CS415 Human Computer Interaction

Programming Languages 2nd edition Tucker and Noonan"

SD Module-1 Advanced JAVA

PIC 20A GUI with swing

SD Module-1 Advanced JAVA. Assignment No. 4

Adapter Pattern Structural

Computer Programming Lecture 11 이윤진서울대학교

Transcription:

Event Binding Different Approaches Global Hooks 2.5 Event Binding 1

Event Dispatch vs. Event Binddling Event Dispatch phase addresses: - Which window receives an event? - Which widget processes it? Positional dispatch Focus dispatch Event Binding answers: - After dispatch to a widget, how do we bind an event to code? Event Capture Event Queue Event Dispatch Event Binding 2.5 Event Binding 2

Event-to-Code Binding How do we design our GUI architecture to enable application logic to interpret events once they ve arrived at the widget? Design Goals: - Easy to understand (clear connection between each event and code that will execute) - Easy to implement (binding paradigm or API) - Easy to debug (how did this event get here?) - Good performance 2.5 Event Binding 3

Approaches Event loop + Switch statement manual binding Switch statement binding Inheritance binding Listener Interface binding Listener Object binding Listener Adapter binding Delegate binding (C#) 2.5 Event Binding 4

Event Loop and Switch Statement Binding All events consumed in one event loop (not by widgets) Switch selects window and code to handle the event Used in Xlib and many early systems while( true ) { XNextEvent(display, &event); // wait next event switch(event.type) { case Expose: //... handle expose event... cout << event.xexpose.count << endl; break; case ButtonPress: //... handle button press event... cout << event.xbutton.x << endl; break;... 2.5 Event Binding 5

Switch Statement Binding: WindowProc Each window registers a WindowProc function (Window Procedure) which is called each time an event is dispatched The WindowProc uses a switch statement to identify each event that it needs to handle. - There are over 100 standard events LRESULT CALLBACK WindowProc(HWND hwnd, UINT umsg, WPARAM wparam, LPARAM lparam) { switch (umsg) { case WM_CLOSE: DestroyWindow (hwnd); break; case WM_SIZE: case WM_KEYDOWN: 2.5 Event Binding 6

Java Event Queue Java has an event queue, can use it like an event loop Available from java.awt.toolkit: - Toolkit.getDefaultToolkit().getSystemEventQueue() java.awt.eventqueue has methods for: - Getting current event, next event - Peeking at an event - Replacing an event (push()) - Placing an event on the queue for later invocation 2.5 Event Binding 7

EventLoop.java public class EventLoop extends JPanel { EventLoop() { EventQueue eq = Toolkit.getDefaultToolkit(). getsystemeventqueue(); eq.push(new MyEventQueue()); public static void main(string[] args) { EventLoop panel = new EventLoop();... private class MyEventQueue extends EventQueue { public void dispatchevent(awtevent e) { if (e.getid() == MouseEvent.MOUSE_DRAGGED) { MouseEvent me = (MouseEvent)e; x = me.getx(); y = me.gety(); repaint(); super.dispatchevent(e); 2.5 Event Binding 8

Inheritance Binding Event is dispatched to an Object-Oriented (OO) widget - OO widget inherits from a base widget class with all event handling methods defined a priori onmousepress, onmousemove, onkeypress, etc - The widget overrides methods for events it wishes to handle - Each method handles multiple related events Used in Java 1.0 2.5 Event Binding 9

InheritanceEvents.java public class InheritanceEvents extends JPanel { public static void main(string[] args) { InheritanceEvents p = new InheritanceEvents();... // enable events for this JPanel p.enableevents(mouseevent.mouse_motion_event_mask); protected void processmousemotionevent(mouseevent e) { // only detects button state WHILE moving! if (e.getid() == MouseEvent.MOUSE_DRAGGED) { x = e.getx(); y = e.gety(); repaint(); 2.5 Event Binding 10

Inheritance Problems Each widget handles its own events, or the widget container has to check what widget the event is meant for Multiple event types are processed through each event method - complex and error-prone, just a switch statement again No filtering of events: performance issues - consider frequent events like mouse-move It doesn t scale well: How to add new events? - e.g. penbuttonpress, touchgesture,. Muddies separation between GUI and application logic: event handling application code is in the inherited widget - Use inheritance for extending functionality, not binding events 2.5 Event Binding 11

Event Interfaces Define an Interface for event handling - collection of method signatures for handling specific events - e.g. an Interface for handling mouse events Can then create a class that implements that interface by implementing methods for handling these mouse events 2.5 Event Binding 12

Listener Interface Binding (Java) Widget object implements event listener Interfaces - e.g. MouseListener, MouseMotionListener, KeyListener, When event is dispatched, relevant listener method is called - mousepressed, mousemoved, 2.5 Event Binding 13

InterfaceEvents.java public class InterfaceEvents extends JPanel implements MouseMotionListener { public static void main(string[] args) { InterfaceEvents panel = new InterfaceEvents();... InterfaceEvents() { this.addmousemotionlistener(this); // add listener public void mousedragged(mouseevent e) { x = e.getx(); y = e.gety(); repaint(); public void mousemoved(mouseevent e) { /* no-op */ 2.5 Event Binding 14

Listener Interface Better, But Still Problems Improvements: - Each event type assigned to an event method - Events are filtered: only sent to object implementing interface - Easy to scale to new events, just add new interfaces e.g. PenInputListener, TouchGestureListener Problems: - Each widget handles its own events, or widget container has to check what widget the event is meant for (i.e. no mediator) - Muddies separation between GUI and application logic: event handling application code is in inherited widget 2.5 Event Binding 15

Listener Object Binding (Java 1.1) Widget object is associated with one or more event listener objects (which implement an event binding interface) - e.g. MouseListener, MouseMotionListener, KeyListener, When event is dispatched to a widget, the relevant listener object processes the event with implemented method: mousepressed, mousereleased, application logic and event handling are decoupled 2.5 Event Binding 16

ListenerEvents.java public class ListenerEvents extends JPanel { public static void main(string[] args) { ListenerEvents panel = new ListenerEvents();... ListenerEvents() { this.addmousemotionlistener(new MyListener()); // inner class listener class MyListener implements MouseMotionListener { public void mousedragged(mouseevent e) { x = e.getx(); y = e.gety(); repaint(); public void mousemoved(mouseevent e) { /* no-op */ 2.5 Event Binding 17

Listener Adapter Binding Many listener interfaces have only a single method - e.g. ActionListener has only actionperformed Other listener interfaces have several methods - e.g. WindowListener has 7 methods, including windowactivated, windowclosed, windowclosing, Typically interested in only a few of these methods. Leads to lots of boilerplate code with no-op methods, e.g. - void windowclosed(windowevent e) { Each listener with multiple methods has an Adapter class with no-op methods. Simply extend the adapter, overriding only the methods of interest. 2.5 Event Binding 18

AdapterEvents.java public class AdapterEvents extends JPanel { public static void main(string[] args) { AdapterEvents panel = new AdapterEvents();... AdapterEvents() { this.addmousemotionlistener(new MyListener()); class MyListener extends MouseMotionAdapter { public void mousedragged(mouseevent e) { x = e.getx(); y = e.gety(); repaint(); 2.5 Event Binding 19

Delegate Binding (.NET) Interface architecture can be a bit heavyweight Can instead have something closer to a simple function callback (a function called when a specific event occurs) Delegates in Microsoft s.net are like a C/C++ function pointer for methods, but they: - Are object oriented - Are completely type checked - Are more secure - Support multicasting (able to point to more than one method) Using delegates is a way to broadcast and subscribe to events.net has special delegates called events 2.5 Event Binding 20

Using Delegates 1. Declare a delegate using a method signature public delegate void Del(string message); 2. Declare a delegate object Del handler; 3. Instantiate the delegate with a method // method to delegate (in MyClass) public static void MyMethod(string message) { System.Console.WriteLine(message); handler = myclassobject.mymethod; 4. Invoke the delegate handler( Hello World ); 2.5 Event Binding 21

Multicasting Instantiate more than one method for a delegate object handler = MyMethod1 + MyMethod2; handler += MyMethod3; Invoke the delegate, calling all the methods handler( Hello World ); Remove method from a delegate object handler -= MyMethod1; What about this? handler = MyMethod4; 2.5 Event Binding 22

Events in.net Events are a delegate with restricted access Declare an event object instead of a delegate object: public delegate void Del(Object o, EventArgs e); event Del handler; event keyword allows enclosing class to use delegate as normal, but outside code can only use the -= and += features of the delegate Gives enclosing class exclusive control over the delegate Outside code can t wipe out delegate list, can t do this: handler = MyMethod4; Can have anonymous delegate events (similar to Java style): b.click += delegate(object o, EventArgs e) { Windows.Forms.MessageBox.Show("Click!"); ; 2.5 Event Binding 23

Global Event Queue Hooks An application monitors BWS events across all applications Can also inject events to another application This can be a very useful technique - examples? This can be a security issue - examples? Take a look at jnativehook - library to provide global keyboard and mouse listeners for Java. - https://github.com/kwhat/jnativehook/ 2.5 Event Binding 24

Global Hooks for Awareness Some application monitor level of activity using global hooks When activity drops, can do something - IM client: set state to away - Screensaver: start screensaver 2.5 Event Binding 25

Events for High Frequency Input Pen and touch generate many high frequency events - pen motion input can be 120Hz or higher - pen sensor is much higher resolution than display - multi-touch generates simultaneous events for multiple fingers Problem: These events often too fast for application to handle Solution: Not all events delivered individually: - e.g. all pendown and penup, but may skip some penmove events - Event object includes array of skipped penmove positions - (Android does this for touch input) 2.5 Event Binding 26

Global Hooks in Tap-Kick-Click research prototype - https://youtu.be/pqycjwhoi2w 2.5 Event Binding 27

Surface Pro 4 vs ipad Pro pencil tracking - https://www.youtube.com/watch?v=pk41eaynlu4 2.5 Event Binding 28