Keyboard and Events. What about the keyboard? Keyboard inputs can be used in many ways---not just for text The boolean variable keyPressed is true if.

Slides:



Advertisements
Similar presentations
Creative Computing. \\ aims By the end of the session you will be able to: 1.Move objects around 2.Write simple interactive programs 3.Use the mouse position.
Advertisements

Introduction to Macromedia Director 8.5 – Lingo
Variables Conditionals Boolean Expressions Conditional Statements How a program produces different results based on varying circumstances if, else if,
Lesson One: The Beginning Chapter 3: Interaction Learning Processing: by Daniel Shiffman Presentation by Donald W. Smith Graphics from text.
Introduction to Programming
Game with US Beginner Tutorial. Welcome!! Who I am What is Processing? Basic Coding Input Methods Images Classes Arrays.
A Quick Introduction to Processing
Emerging Platform#5: Processing 2 B. Ramamurthy 6/13/2014B. Ramamurthy, CS6511.
© Calvin College, Being abstract is something profoundly different from being vague... The purpose of abstraction is not to be vague, but to create.
 Use the Left and Right arrow keys or the Page Up and Page Down keys to move between the pages. You can also click on the pages to move forward.  To.
 Variables  What are they?  Declaring and initializing variables  Common uses for variables  Variables you get “for free” in Processing ▪ Aka: Built-in.
Processing Processing is a simple programming environment that was created to make it easier to develop visually oriented applications with an emphasis.
IAT 334 Java using Processing ______________________________________________________________________________________ SCHOOL OF INTERACTIVE ARTS + TECHNOLOGY.
IAT 800 Lab 1: Loops, Animation, and Simple User Interaction.
ICM Week 2. Structure - statements and blocks of code Any single statement ends with semicolon ; When we want to bunch a few statements together we use.
Event Handling. In this class we will cover: Keyboard Events Mouse Events Focus Events Action Interface Multicasting.
Lecture 3 IAT 800. Sept 15, Fall 2006IAT 8002 Suggestions on learning to program  Spend a lot of time fiddling around with code –Programming is something.
PROCESSING Animation. Objectives Be able to create Processing animations Be able to create interactive Processing programs.
Review Blocks of code {.. A bunch of ‘statements’; } Structured programming Learning Processing: Slides by Don Smith 1.
Programming for Artists ART 315 Dr. J. R. Parker Art/Digital Media Lab Lec 10 Fall 2010.
1 k Jarek Rossignac,  2008 Processing  Install Processing  Learn how to edit, run, save, export, post programs  Understand.
Continuous February 16, Test Review What expression represents the zip car eligibility rules of at least 18 years old and no incidents?
Processing Lecture.2 Mouse and Keyboard
1. Chapter 4 Customizing Paragraphs 3 More Paragraph Changes Highlight a paragraph in Word by applying borders and shading. Sort paragraphs to control.
Review of ArT3 Programming Course David Meredith Aalborg University
EVENT-DRIVEN PROGRAMMING. โปรแกรมและอุปกรณ์ส่วนมากที่ใช้ใน ชีวิตประจำวัน จะตอบสนองกับเหตุการณ์ที่ เกิดขึ้น ตัวอย่างของเหตุการณ์ อาทิ การเคลื่อน หรือ คลิกเมาส์
______________________________________________________________________________________ SCHOOL OF INTERACTIVE ARTS + TECHNOLOGY [SIAT] |
Piñata Game: Keeping Score in Alice By Maggie Bashford Professor Susan Rodger Duke University July
1 Georgia Tech, IIC, GVU, 2006 MAGIC Lab Rossignac Lecture 02b: Tutorial for Programming in Processing Jarek Rossignac.
B. RAMAMURTHY Simulating Motion and Implementing Animation.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 21.1 Test-Driving the Painter Application.
CIS 3.5 Lecture 2.2 More programming with "Processing"
Lesson Two: Everything You Need to Know
Variables Art &Technology, 3rd Semester Aalborg University Programming David Meredith
Arrays. An array is a collection “The Dinner offers an array of choices.” In computer programming, an array is a collection of variables of the same data.
Agenda Introduction. Event Model. Creating GUI Application. Event Examples.
Mouse Inputs in Processing. Interacting with the Mouse mouseX and mouseY: pg mouseXmouseY –The position of the mouse in the canvas pmouseX and.
Lesson Two: Everything You Need to Know
Lawrence Snyder University of Washington, Seattle © Lawrence Snyder 2004 More details and explanation …
Test 2 Review. General Info. All tests are comprehensive. You are still responsible for the material covered prior to the first exam. You will be tested.
Often being different. Control flow By default Java (and therefore Processing) executes lines of a program one after the other –Doesn’t matter what happened.
Words. Characters and Strings Character –A single character inside of single quotes char letter = 'A' ; char digit = '0' ; – Strings Zero or more character.
Computer Science I Recap: variables & functions. Images. Pseudo-random processing.
Computer Science I Looping. User input. Classwork/Homework: Incorporate looping & user input into a sketch.
G RAPHICS & I NTERACTIVE P ROGRAMMING Lecture 2 More Programming with Processing.
Continuous. Flow of Control Programs can broadly be classified as being –Procedural Programs are executed once in the order specified by the code varied.
B. RAMAMURTHY Processing and Programming cse
Test Review. General Info. All tests will be comprehensive. You will be tested more on your understanding of code as opposed to your ability to write.
Welcome to Processing Who does not have access to digital camera?
1 SIC / CoC / Georgia Tech MAGIC Lab Rossignac Processing  Install Processing  Learn how to edit, run, save, export,
What is Computing? What can be Programmed? Creative Computing Processing Downloading Processing Dropbox Primitive Shapes – point – line – triangle – quad.
Review Random numbers mouseX, mouseY setup() & draw() frameRate(), loop(), noLoop() Mouse and Keyboard interaction Arcs, curves, bézier curves, custom.
Computer Science I Animations. Bouncing ball. The if statement. Classwork/homework: bouncing something. Compress and upload work to Moodle.
Computer Science I Go over midterm. Reprise on curves. Table examples. The map function. Testing Strings. Fonts. Classwork/Homework: Complete midterm project.
Computer Science I More class examples. Paths. Jigsaw. Tolerance. Classwork/homework: Your class project. Post proposal for midterm project.
Variables. Something to mention… void setup(){ size(200, 200); background(255); smooth(); } void draw() { stroke(0); strokeWeight(abs(mouseX-pmouseX));
Loops. About the Midterm Exam.. Exam on March 12 Monday (tentatively) Review on March 5.
Basic Input and Output CSE 120 Spring 2017
p5.js mouse, keyboard and if’s
Event Driven Programming
Computation as an Expressive Medium
Basic Input and Output CSE 120 Winter 2018
Mouse Inputs in Processing
Chapter 5, Conditionals Brief Notes
More programming with "Processing"
A Few Notes Starting August 29, 2018
Announcements How’s it going? My internet/power supply?
Basic Input and Output CSE 120 Winter 2019
LCC 6310 Computation as an Expressive Medium
Continuous & Random September 21, 2009.
Presentation transcript:

Keyboard and Events

What about the keyboard? Keyboard inputs can be used in many ways---not just for text The boolean variable keyPressed is true if a key is pressed and false if not. keyPressed is true as long as a key is held down // Draw a line when any key // is pressed void setup() { size(100, 100); smooth(); strokeWeight(4); } void draw() { background(204); if (keyPressed == true) { line(20, 20, 80, 80); }

Another example with keyPressed // Move a line while any key is pressed int x = 20; void setup() { size(100, 100); smooth(); strokeWeight(4); } void draw() { background(204); if (keyPressed == true) { // If the key is pressed x++; // add 1 to x } line(x, 20, x-60, 80); }

You can also display text The key variable is of type char and stores the most recently pressed key PFont font; void setup() { size(100, 100); font = loadFont("Tahoma-Bold-18.vlw"); textFont(font); } void draw() { background(0); text(key, 28, 75); }

But characters are really numbers Each character has a numerical value defined by the ASCII codeASCII code int x = 0; void setup() { size(100, 100); } void draw() { if (keyPressed == true) { x = key - 48; rect(x, -1, 20, 101); }

Coded Keys Processing can also read the value from other keys such as the arrow keys, Alt, Shift, Backspace, Tab and others (see page 227) Check that the key is coded first: key==CODED color y = 35; void setup() { size(100, 100); } void draw() { background(204); line(10, 50, 90, 50); if (key == CODED) { if (keyCode == UP) { y = 20; } else if (keyCode == DOWN) { y = 50; } } else { y = 35; } rect(25, y, 50, 30); }

In-class exercise Use the arrow keys to change the position of a shape within the canvas.

Flow of Control Programs can broadly be classified as being –Procedural Programs are executed once in the order specified by the code varied only by loops and function calls. –Event Driven Programs run continuously responding to input events such as key strokes or mouse clicks.

Refresher… Today we will review events for event driven programs. First event driven program void draw() { frameRate(4); //fps = 4 println(frameCount); }

What Happened? About 4 times per second, a number (the frame count) was printed to the console window. Why? –There’s no for loop or while loop? The draw() function is processed continuously by the event handler until another event is triggered or you press the STOP button.

More on Why Specifically the draw() function is called 4 times per second since we set the frameRate to 4. Remove the frameRate() line and see what happens. What’s the default frame rate?

Next Program float gray = 0; void setup() { size(100, 100); } void draw() { background(gray); } void mousePressed() { gray += 20; }

Change It Change the mousePressed() to mouseReleased(). –What happens differently? Move the background() call to mouseReleased(). Now draw() is empty? Can we remove it? –Why or why not?

draw() Event Driven Programs –Programs run continuously responding to input events such as key strokes or mouse clicks. Without the draw() function, our program is no longer listening for events.

For Something Different void setup() { size(100, 100); fill(0, 102); } void draw() { } //Empty draw() keeps program running void mousePressed() { rect(mouseX, mouseY, 33, 33); }

mouseMoved() & mouseDragged() What’s the difference between a dragged mouse and a moved mouse? If you don’t know, run this program and find out! int dragX, dragY, moveX, moveY; void setup() { size(100, 100); smooth(); noStroke(); }//continues on next slide

mouseMoved() & mouseDragged() [continued] void draw() { background(204); fill(0); ellipse(dragX, dragY, 33, 33); // Black circle fill(153); ellipse(moveX, moveY, 33, 33); // Gray circle } void mouseMoved() { // Move gray circle moveX = mouseX; moveY = mouseY; } void mouseDragged() { // Move black circle dragX = mouseX; dragY = mouseY; }

keyPressed() & keyReleased() boolean drawT = false; void setup() { size(100, 100); noStroke(); } void draw() { background(204); if (drawT == true) { rect(20, 20, 60, 20); rect(39, 40, 22, 45); } } //continued on next slide

keyPressed() & keyReleased() void keyPressed() { if ((key == 'T') || (key == 't')) { drawT = true; } void keyReleased() { drawT = false; }

Using Strings // An extremely minimal text editor, it can only insert // and remove characters from a single line PFont font; String letters = ""; void setup() { size(100, 100); font = loadFont("Eureka-24.vlw"); textFont(font); stroke(255); fill(0); } void draw() { background(204); float cursorPosition = textWidth(letters); line(cursorPosition, 0, cursorPosition, 100); text(letters, 0, 50); } // continued on next slide

Using Strings void keyPressed() { if (key == BACKSPACE) { // Backspace if (letters.length() > 0) { letters = letters.substring(0, letters.length()-1); } } else if (textWidth(letters+key) < width){ letters = letters+key; } What do you think letters.substring(), letters.length and textWidth() do?

letters.substring(), letters.length() and textWidth() letters.substring() – letters.length() – textWidth() –

Flow Control frameRate() –Sets a limit as to how many frames are displayed per second loop() –Resumes continuous draw() calls noLoop() –Stops draw() from repeated being called redraw() –Calls draw() once

int frame = 0; void setup() { size(100, 100); frameRate(30); } void draw() { if (frame > 60) { // If 60 frames since the mouse noLoop(); // was pressed, stop the program background(0); // and turn the background black. } else { // Otherwise, set the background background(204); // to light gray and draw lines line(mouseX, 0, mouseX, 100); // at the mouse position line(0, mouseY, 100, mouseY); frame++; } void mousePressed() { loop(); frame = 0; }

Run This and Then Remove Comments in setup() void setup() { size(100, 100); //noLoop(); // what happens when noLoop is uncommented } void draw() { background(204); line(mouseX, 0, mouseX, 100); } void mousePressed() { redraw(); // Run the code in draw one time }

Eventful review Classes of events –Mouse –Key –Timer (alarm)

Mouse event handlers mousePressed() –Called once when any mouse button is pressed mouseReleased() –Called once when any mouse button is released mouseMoved() –Called often while mouse is moved mouseDragged() –Called often while mouse is moved with button pressed An example to try out

Key event handlers keyPressed() –Called once when any key is pressed –Be careful about keys that are held down! keyReleased() –Called once when any key is released

Timer events and their controls draw() –Called at regular intervals frameRate(freq) –Sets the interval to 1/ freq seconds noloop() –Turns off the regular calls to draw() loop() –Turns on the regular calls to draw() redraw() –Makes a single call to draw()

Your Turn Create a shape that has events defined for mousePressed(), mouseDragged(), mouseReleased() and keyPressed() where key equals some particular value. For example an ‘L’ draws a line.