SlideShare a Scribd company logo
1 of 13
Mohammed
AlTayer
KEYBOARD AND MOUSE
EVENTS IN PYTHON
The simplest mouse interaction is to wait for the user to click
before proceeding in the program. Suppose the 3D display is in
scene, the default window created by VPython. Here is a way to
wait for a mouse click, which is defined as the mouse button
being pressed and released without moving the mouse (the
event occurs when the mouse button is released)
MOUSE INTERACTIONS
The mouse routines can handle a three-button mouse, with
"left", "right", and "middle" buttons. For systems with a two-
button mouse, the "middle" button consists of the left and right
buttons pressed together. For the Macintosh one-button mouse,
the right button is invoked by holding down the Command key
(normally used for rotating the camera view), and the middle
button is invoked by holding down the Option key (normally used
for zooming the camera view).
DIFFERENT KINDS OF MOUSE
VPython continues to provide the basic mouse event
functionality for handling events from right and middle buttons
when userspin or userzoom is disabled, out of concern for
supporting old programs. However, it has become evident that
there are limitations to this approach which could preclude
some kinds of mouse handling that people might want to do in
the future. For example, you might want to allow userspin with
right drags yet also pick up right clicks. For that reason it is
conceivable that future developments in this area might break
existing programs, and therefore for maximum forward
compatibility it is prudent to use only left-button interactions in
new programs.
DESIGN FOR LEFT-BUTTON EVENTS IF
POSSIBLE
There are two different ways to get a mouse event, "polling" and
"callback". In polling, you continually check scene.mouse.events
to see whether any events are waiting to be processed, and you
use scene.mouse.getevent() to get the next event to process.
Prior to VPython 6, this was the only way you could handle
mouse or keyboard events.
If you use the callback method, you specify a function to be
executed when a specific type of event occurs, and the function
is sent the event information when the specified type of event
occurs. For many purposes this is a better way to handle mouse
and keyboard events, and we will discuss it first. Programs that
use polling will continue to work, but you cannot mix polling and
callback approaches: you must use one or the other in a
program.
POLLING AND CALLBACK
Here is a simple example of how to use callbacks to process
click events:
from visual import *
s = sphere(color=color.cyan)
def change():
if s.color == color.cyan:
s.color = color.red
else:
s.color = color.cyan
scene.bind('click', change)
We define a "function" named "change". Then we "bind" this
function to click events occurring in the display named
"scene". Whenever VPython detects that a click event has
HANDLING EVENTS WITH CALLBACKS
You can get detailed information about the event by writing the
callback function like this (note the variable 'evt' in parentheses):
def info(evt):
print(evt.event, evt.pos, evt.button)
Here we specify an argument in the definition of the callback
function ('evt' in this case). When the function is called due to a
specified event happening, VPython sends the function the
information contained in scene.mouse, plus 'event', which is the
name of the event that triggered the callback, such as
'mousedown' or 'click'. The name of the argument need not be
'evt'; use whatever name you like. In addition to evt.event and
evt.button, there is further event information in the form of
evt.press, evt.click, evt.drag, evt.drop, and evt.release (see details
in the section on polling), but this information is more relevant
when using polling rather than callbacks to get events.
DETAILS OF THE EVENT
Normally, only the left mouse button will trigger an event, but if
you specify scene.userspin = False, so the right button is no
longer bound to camera rotation, clicking with the right mouse
button will cause a callback. Similarly, if you specify
scene.userzoom = False, you can click with the middle button
(or left+right buttons).
RIGHT OR MIDDLE BUTTON MOUSE EVENTS
Suppose you executed scene.bind('mousedown mousemove', Drag),
but now you no longer want to send mousemove events to that
function. Do this:
scene.unbind('mousemove', Drag)
You can also leave a function bound but start and stop having
events sent to it:
D = scene.bind('mousemove', Drag)
...
D.stop() # temporarily stop events going to Drag
...
D.start() # start sending events to Drag again
You can check whether the callback is in start or stop mode with
D.enabled, which is True if the callback has been started and False
if it has been stopped.
UNBINDING
It is possible to create your own event type, and trigger a callback function to do
something. Consider the following example, where the event type is ' color_the_ball':
def clickFunc():
s = sphere(pos=scene.mouse.pos, radius=0.1)
scene.trigger('color_the_ball', s)
def ballFunc(newball):
newball.color=color.cyan
scene.bind('click', clickFunc)
scene.bind('color_the_ball', ballFunc)
box(pos=(1,0,0))
We bind click events to the function clickFunc, and we bind our own special event type
'color_the_ball' to the function ballFunc. The function clickFunc is executed when the
user clicks the mouse. This function creates a small sphere at the location of the
mouse click, then triggers an event ' color_the_ball', with the effect of passing to the
function ballFunc the sphere object. Finally ballFunc applies a color to the sphere.
(Obviously one could color the sphere in clickFunc; the example is just for illustration
of the basic concept.)
CUSTOM EVENTS: TRIGGERS
The following information on how to handle events using polling
is still valid, but you are encouraged to consider using the more
powerful callback approach when writing new programs.
Remember that you cannot mix the two schemes. You can use
either callback or polling in a program, but not both.
The simplest polling mouse interaction is to wait for a mouse
click:
scene.mouse.getclick() Wait for a mouse click. If you say m =
scene.mouse.getclick(), the variable m gives information about
the event. For example, m.pos is the location of the mouse at
the time of the click event.
HANDLING EVENTS WITH POLLING
 ere is a way to get the mouse position relative to a particular plane in
space:
 temp = scene.mouse.project(normal=(0,1,0),point=(0,3,0))
 if temp: # temp is None if no intersection with plane
 ball.pos = temp
 Here a plane is specified by its normal and a point in the plane, and if
point is not specified, the plane passes through the origin. You obtain the
3D location in the plane where the user sees the mouse cursor. If the
projection of the mouse misses the plane, as would happen if the plane is
seen edge-on, the result is the special Python value None.
 In the example shown above, the user of your program will be able to use
the mouse to place balls in a plane parallel to the xy plane, a height of 3
above the xy plane, no matter how the user has rotated the point of view.
 You can instead specify a perpendicular distance d from the origin to the
plane that is perpendicular to the specified normal. The example above is
equivalent to
 temp = scene.mouse.project(normal=(0,1,0), d=3)
PROJECTING MOUSE POSITION ONTO A GIVEN
PLANE
Often you want to pause for either mouse or keyboard input. You
can copy the following function into your program, and then insert
pause() wherever you want to pause.
def pause():
while True:
rate(30)
if scene.mouse.events:
m = scene.mouse.getevent()
if m.click == 'left': return
elif scene.kb.keys:
k = scene.kb.getkey()
return
As of VPython 6, an alternative to this function is simply to write
scene.waitfor('click keydown').
PAUSING FOR MOUSE OR KEYBOARD INPUT

More Related Content

What's hot

Getting touchy - an introduction to touch events / Sainté Mobile Days / Saint...
Getting touchy - an introduction to touch events / Sainté Mobile Days / Saint...Getting touchy - an introduction to touch events / Sainté Mobile Days / Saint...
Getting touchy - an introduction to touch events / Sainté Mobile Days / Saint...Patrick Lauke
 
Guida Uso V Teng
Guida Uso V TengGuida Uso V Teng
Guida Uso V TengJacekKupras
 
Star logo nova code cookbook
Star logo nova  code cookbookStar logo nova  code cookbook
Star logo nova code cookbookBarbara M. King
 
дыдыкин егор
дыдыкин егордыдыкин егор
дыдыкин егорkuchinskaya
 
Getting touchy - an introduction to touch and pointer events / Future of Web ...
Getting touchy - an introduction to touch and pointer events / Future of Web ...Getting touchy - an introduction to touch and pointer events / Future of Web ...
Getting touchy - an introduction to touch and pointer events / Future of Web ...Patrick Lauke
 
Introduction to programming - class 5
Introduction to programming - class 5Introduction to programming - class 5
Introduction to programming - class 5Paul Brebner
 
Getting touchy - an introduction to touch and pointer events / Frontend NE / ...
Getting touchy - an introduction to touch and pointer events / Frontend NE / ...Getting touchy - an introduction to touch and pointer events / Frontend NE / ...
Getting touchy - an introduction to touch and pointer events / Frontend NE / ...Patrick Lauke
 
Getting touchy - an introduction to touch and pointer events (complete master...
Getting touchy - an introduction to touch and pointer events (complete master...Getting touchy - an introduction to touch and pointer events (complete master...
Getting touchy - an introduction to touch and pointer events (complete master...Patrick Lauke
 
Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2...
Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2...Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2...
Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2...Patrick Lauke
 
Introductory manual for the open source meshing code SALOME
Introductory manual for the open source meshing code SALOMEIntroductory manual for the open source meshing code SALOME
Introductory manual for the open source meshing code SALOMEFilippos Kalofotias
 
Java Event Handling
Java Event HandlingJava Event Handling
Java Event HandlingShraddha
 

What's hot (18)

Paper Ball
Paper BallPaper Ball
Paper Ball
 
What is Event
What is EventWhat is Event
What is Event
 
The touch events
The touch eventsThe touch events
The touch events
 
Getting touchy - an introduction to touch events / Sainté Mobile Days / Saint...
Getting touchy - an introduction to touch events / Sainté Mobile Days / Saint...Getting touchy - an introduction to touch events / Sainté Mobile Days / Saint...
Getting touchy - an introduction to touch events / Sainté Mobile Days / Saint...
 
Guida Uso V Teng
Guida Uso V TengGuida Uso V Teng
Guida Uso V Teng
 
Star logo nova code cookbook
Star logo nova  code cookbookStar logo nova  code cookbook
Star logo nova code cookbook
 
Sln skill cards
Sln skill cardsSln skill cards
Sln skill cards
 
Rets class
Rets classRets class
Rets class
 
дыдыкин егор
дыдыкин егордыдыкин егор
дыдыкин егор
 
Getting touchy - an introduction to touch and pointer events / Future of Web ...
Getting touchy - an introduction to touch and pointer events / Future of Web ...Getting touchy - an introduction to touch and pointer events / Future of Web ...
Getting touchy - an introduction to touch and pointer events / Future of Web ...
 
Introduction to programming - class 5
Introduction to programming - class 5Introduction to programming - class 5
Introduction to programming - class 5
 
Getting touchy - an introduction to touch and pointer events / Frontend NE / ...
Getting touchy - an introduction to touch and pointer events / Frontend NE / ...Getting touchy - an introduction to touch and pointer events / Frontend NE / ...
Getting touchy - an introduction to touch and pointer events / Frontend NE / ...
 
Getting touchy - an introduction to touch and pointer events (complete master...
Getting touchy - an introduction to touch and pointer events (complete master...Getting touchy - an introduction to touch and pointer events (complete master...
Getting touchy - an introduction to touch and pointer events (complete master...
 
Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2...
Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2...Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2...
Getting touchy - Introduction to touch (and pointer) events / jQuery Europe 2...
 
ملخص البرمجة المرئية - الوحدة السابعة
ملخص البرمجة المرئية - الوحدة السابعةملخص البرمجة المرئية - الوحدة السابعة
ملخص البرمجة المرئية - الوحدة السابعة
 
Introductory manual for the open source meshing code SALOME
Introductory manual for the open source meshing code SALOMEIntroductory manual for the open source meshing code SALOME
Introductory manual for the open source meshing code SALOME
 
Poser presentation1
Poser presentation1Poser presentation1
Poser presentation1
 
Java Event Handling
Java Event HandlingJava Event Handling
Java Event Handling
 

Similar to Keyboard and mouse events in python

Alice05
Alice05Alice05
Alice05h2vs10
 
Actionscript 3 - Session 6 Interactivity
Actionscript 3 - Session 6 InteractivityActionscript 3 - Session 6 Interactivity
Actionscript 3 - Session 6 InteractivityOUM SAOKOSAL
 
Flash auto play image gallery
Flash auto play image galleryFlash auto play image gallery
Flash auto play image galleryBoy Jeorge
 
Python keyboard and mouse events
Python keyboard and mouse eventsPython keyboard and mouse events
Python keyboard and mouse eventsAhmed Alyazji
 
Multimedia lecture ActionScript3
Multimedia lecture ActionScript3Multimedia lecture ActionScript3
Multimedia lecture ActionScript3Mohammed Hussein
 
3.4 events and interactivity
3.4   events and interactivity3.4   events and interactivity
3.4 events and interactivityallenbailey
 
INTERFACCE GRAFICHE CON UNITY3D 4.6: IL GIOCO NON BASTA!
INTERFACCE GRAFICHE CON UNITY3D 4.6: IL GIOCO NON BASTA!INTERFACCE GRAFICHE CON UNITY3D 4.6: IL GIOCO NON BASTA!
INTERFACCE GRAFICHE CON UNITY3D 4.6: IL GIOCO NON BASTA!DotNetCampus
 
Yahoo presentation: JavaScript Events
Yahoo presentation: JavaScript EventsYahoo presentation: JavaScript Events
Yahoo presentation: JavaScript EventsPeter-Paul Koch
 
The following GUI is displayed once the application startsThe sug.pdf
The following GUI is displayed once the application startsThe sug.pdfThe following GUI is displayed once the application startsThe sug.pdf
The following GUI is displayed once the application startsThe sug.pdfarihantsherwani
 
Actionscript 3 - Session 2 Getting Started Flash IDE
Actionscript 3 - Session 2 Getting Started Flash IDEActionscript 3 - Session 2 Getting Started Flash IDE
Actionscript 3 - Session 2 Getting Started Flash IDEOUM SAOKOSAL
 
Getting touchy - an introduction to touch events / Web Standards Days / Mosco...
Getting touchy - an introduction to touch events / Web Standards Days / Mosco...Getting touchy - an introduction to touch events / Web Standards Days / Mosco...
Getting touchy - an introduction to touch events / Web Standards Days / Mosco...Patrick Lauke
 
2d game engine workflow
2d game engine workflow2d game engine workflow
2d game engine workflowluisfvazquez1
 
Getting touchy - an introduction to touch and pointer events / Web Rebels / O...
Getting touchy - an introduction to touch and pointer events / Web Rebels / O...Getting touchy - an introduction to touch and pointer events / Web Rebels / O...
Getting touchy - an introduction to touch and pointer events / Web Rebels / O...Patrick Lauke
 

Similar to Keyboard and mouse events in python (20)

Alice05
Alice05Alice05
Alice05
 
The touch events
The touch eventsThe touch events
The touch events
 
Actionscript 3 - Session 6 Interactivity
Actionscript 3 - Session 6 InteractivityActionscript 3 - Session 6 Interactivity
Actionscript 3 - Session 6 Interactivity
 
Flash auto play image gallery
Flash auto play image galleryFlash auto play image gallery
Flash auto play image gallery
 
Python keyboard and mouse events
Python keyboard and mouse eventsPython keyboard and mouse events
Python keyboard and mouse events
 
Multimedia lecture ActionScript3
Multimedia lecture ActionScript3Multimedia lecture ActionScript3
Multimedia lecture ActionScript3
 
3.4 events and interactivity
3.4   events and interactivity3.4   events and interactivity
3.4 events and interactivity
 
15a gui
15a gui15a gui
15a gui
 
Unity3 d uitools
Unity3 d uitoolsUnity3 d uitools
Unity3 d uitools
 
INTERFACCE GRAFICHE CON UNITY3D 4.6: IL GIOCO NON BASTA!
INTERFACCE GRAFICHE CON UNITY3D 4.6: IL GIOCO NON BASTA!INTERFACCE GRAFICHE CON UNITY3D 4.6: IL GIOCO NON BASTA!
INTERFACCE GRAFICHE CON UNITY3D 4.6: IL GIOCO NON BASTA!
 
Yahoo presentation: JavaScript Events
Yahoo presentation: JavaScript EventsYahoo presentation: JavaScript Events
Yahoo presentation: JavaScript Events
 
The following GUI is displayed once the application startsThe sug.pdf
The following GUI is displayed once the application startsThe sug.pdfThe following GUI is displayed once the application startsThe sug.pdf
The following GUI is displayed once the application startsThe sug.pdf
 
Actionscript 3 - Session 2 Getting Started Flash IDE
Actionscript 3 - Session 2 Getting Started Flash IDEActionscript 3 - Session 2 Getting Started Flash IDE
Actionscript 3 - Session 2 Getting Started Flash IDE
 
Getting touchy - an introduction to touch events / Web Standards Days / Mosco...
Getting touchy - an introduction to touch events / Web Standards Days / Mosco...Getting touchy - an introduction to touch events / Web Standards Days / Mosco...
Getting touchy - an introduction to touch events / Web Standards Days / Mosco...
 
Spf chapter10 events
Spf chapter10 eventsSpf chapter10 events
Spf chapter10 events
 
Event handling in Java(part 1)
Event handling in Java(part 1)Event handling in Java(part 1)
Event handling in Java(part 1)
 
Unity 101
Unity 101Unity 101
Unity 101
 
2d game engine workflow
2d game engine workflow2d game engine workflow
2d game engine workflow
 
Getting touchy - an introduction to touch and pointer events / Web Rebels / O...
Getting touchy - an introduction to touch and pointer events / Web Rebels / O...Getting touchy - an introduction to touch and pointer events / Web Rebels / O...
Getting touchy - an introduction to touch and pointer events / Web Rebels / O...
 
event_handling.ppt
event_handling.pptevent_handling.ppt
event_handling.ppt
 

Recently uploaded

How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaVirag Sontakke
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsKarinaGenton
 
Class 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfClass 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfakmcokerachita
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting DataJhengPantaleon
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxRaymartEstabillo3
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 

Recently uploaded (20)

How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of India
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 
Staff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSDStaff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSD
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its Characteristics
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
Class 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfClass 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdf
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 

Keyboard and mouse events in python

  • 2. The simplest mouse interaction is to wait for the user to click before proceeding in the program. Suppose the 3D display is in scene, the default window created by VPython. Here is a way to wait for a mouse click, which is defined as the mouse button being pressed and released without moving the mouse (the event occurs when the mouse button is released) MOUSE INTERACTIONS
  • 3. The mouse routines can handle a three-button mouse, with "left", "right", and "middle" buttons. For systems with a two- button mouse, the "middle" button consists of the left and right buttons pressed together. For the Macintosh one-button mouse, the right button is invoked by holding down the Command key (normally used for rotating the camera view), and the middle button is invoked by holding down the Option key (normally used for zooming the camera view). DIFFERENT KINDS OF MOUSE
  • 4. VPython continues to provide the basic mouse event functionality for handling events from right and middle buttons when userspin or userzoom is disabled, out of concern for supporting old programs. However, it has become evident that there are limitations to this approach which could preclude some kinds of mouse handling that people might want to do in the future. For example, you might want to allow userspin with right drags yet also pick up right clicks. For that reason it is conceivable that future developments in this area might break existing programs, and therefore for maximum forward compatibility it is prudent to use only left-button interactions in new programs. DESIGN FOR LEFT-BUTTON EVENTS IF POSSIBLE
  • 5. There are two different ways to get a mouse event, "polling" and "callback". In polling, you continually check scene.mouse.events to see whether any events are waiting to be processed, and you use scene.mouse.getevent() to get the next event to process. Prior to VPython 6, this was the only way you could handle mouse or keyboard events. If you use the callback method, you specify a function to be executed when a specific type of event occurs, and the function is sent the event information when the specified type of event occurs. For many purposes this is a better way to handle mouse and keyboard events, and we will discuss it first. Programs that use polling will continue to work, but you cannot mix polling and callback approaches: you must use one or the other in a program. POLLING AND CALLBACK
  • 6. Here is a simple example of how to use callbacks to process click events: from visual import * s = sphere(color=color.cyan) def change(): if s.color == color.cyan: s.color = color.red else: s.color = color.cyan scene.bind('click', change) We define a "function" named "change". Then we "bind" this function to click events occurring in the display named "scene". Whenever VPython detects that a click event has HANDLING EVENTS WITH CALLBACKS
  • 7. You can get detailed information about the event by writing the callback function like this (note the variable 'evt' in parentheses): def info(evt): print(evt.event, evt.pos, evt.button) Here we specify an argument in the definition of the callback function ('evt' in this case). When the function is called due to a specified event happening, VPython sends the function the information contained in scene.mouse, plus 'event', which is the name of the event that triggered the callback, such as 'mousedown' or 'click'. The name of the argument need not be 'evt'; use whatever name you like. In addition to evt.event and evt.button, there is further event information in the form of evt.press, evt.click, evt.drag, evt.drop, and evt.release (see details in the section on polling), but this information is more relevant when using polling rather than callbacks to get events. DETAILS OF THE EVENT
  • 8. Normally, only the left mouse button will trigger an event, but if you specify scene.userspin = False, so the right button is no longer bound to camera rotation, clicking with the right mouse button will cause a callback. Similarly, if you specify scene.userzoom = False, you can click with the middle button (or left+right buttons). RIGHT OR MIDDLE BUTTON MOUSE EVENTS
  • 9. Suppose you executed scene.bind('mousedown mousemove', Drag), but now you no longer want to send mousemove events to that function. Do this: scene.unbind('mousemove', Drag) You can also leave a function bound but start and stop having events sent to it: D = scene.bind('mousemove', Drag) ... D.stop() # temporarily stop events going to Drag ... D.start() # start sending events to Drag again You can check whether the callback is in start or stop mode with D.enabled, which is True if the callback has been started and False if it has been stopped. UNBINDING
  • 10. It is possible to create your own event type, and trigger a callback function to do something. Consider the following example, where the event type is ' color_the_ball': def clickFunc(): s = sphere(pos=scene.mouse.pos, radius=0.1) scene.trigger('color_the_ball', s) def ballFunc(newball): newball.color=color.cyan scene.bind('click', clickFunc) scene.bind('color_the_ball', ballFunc) box(pos=(1,0,0)) We bind click events to the function clickFunc, and we bind our own special event type 'color_the_ball' to the function ballFunc. The function clickFunc is executed when the user clicks the mouse. This function creates a small sphere at the location of the mouse click, then triggers an event ' color_the_ball', with the effect of passing to the function ballFunc the sphere object. Finally ballFunc applies a color to the sphere. (Obviously one could color the sphere in clickFunc; the example is just for illustration of the basic concept.) CUSTOM EVENTS: TRIGGERS
  • 11. The following information on how to handle events using polling is still valid, but you are encouraged to consider using the more powerful callback approach when writing new programs. Remember that you cannot mix the two schemes. You can use either callback or polling in a program, but not both. The simplest polling mouse interaction is to wait for a mouse click: scene.mouse.getclick() Wait for a mouse click. If you say m = scene.mouse.getclick(), the variable m gives information about the event. For example, m.pos is the location of the mouse at the time of the click event. HANDLING EVENTS WITH POLLING
  • 12.  ere is a way to get the mouse position relative to a particular plane in space:  temp = scene.mouse.project(normal=(0,1,0),point=(0,3,0))  if temp: # temp is None if no intersection with plane  ball.pos = temp  Here a plane is specified by its normal and a point in the plane, and if point is not specified, the plane passes through the origin. You obtain the 3D location in the plane where the user sees the mouse cursor. If the projection of the mouse misses the plane, as would happen if the plane is seen edge-on, the result is the special Python value None.  In the example shown above, the user of your program will be able to use the mouse to place balls in a plane parallel to the xy plane, a height of 3 above the xy plane, no matter how the user has rotated the point of view.  You can instead specify a perpendicular distance d from the origin to the plane that is perpendicular to the specified normal. The example above is equivalent to  temp = scene.mouse.project(normal=(0,1,0), d=3) PROJECTING MOUSE POSITION ONTO A GIVEN PLANE
  • 13. Often you want to pause for either mouse or keyboard input. You can copy the following function into your program, and then insert pause() wherever you want to pause. def pause(): while True: rate(30) if scene.mouse.events: m = scene.mouse.getevent() if m.click == 'left': return elif scene.kb.keys: k = scene.kb.getkey() return As of VPython 6, an alternative to this function is simply to write scene.waitfor('click keydown'). PAUSING FOR MOUSE OR KEYBOARD INPUT