| 42333 |
ripley |
1 |
% File src/library/grDevices/man/getGraphicsEvent.Rd
|
|
|
2 |
% Part of the R package, http://www.R-project.org
|
| 59039 |
ripley |
3 |
% Copyright 1995-2007 R Core Team
|
| 42333 |
ripley |
4 |
% Distributed under GPL 2 or later
|
|
|
5 |
|
| 38051 |
ripley |
6 |
\name{getGraphicsEvent}
|
| 56186 |
murdoch |
7 |
\alias{getGraphicsEvent}
|
| 52081 |
murdoch |
8 |
\alias{setGraphicsEventHandlers}
|
|
|
9 |
\alias{getGraphicsEventEnv}
|
|
|
10 |
\alias{setGraphicsEventEnv}
|
| 38051 |
ripley |
11 |
\title{Wait for a mouse or keyboard event from a graphics window}
|
|
|
12 |
\description{
|
|
|
13 |
This function waits for input from a graphics window in the
|
|
|
14 |
form of a mouse or keyboard event.
|
|
|
15 |
}
|
|
|
16 |
\usage{
|
| 61433 |
ripley |
17 |
getGraphicsEvent(prompt = "Waiting for input",
|
| 47621 |
ripley |
18 |
onMouseDown = NULL, onMouseMove = NULL,
|
| 52081 |
murdoch |
19 |
onMouseUp = NULL, onKeybd = NULL,
|
|
|
20 |
consolePrompt = prompt)
|
|
|
21 |
setGraphicsEventHandlers(which = dev.cur(), ...)
|
|
|
22 |
getGraphicsEventEnv(which = dev.cur())
|
|
|
23 |
setGraphicsEventEnv(which = dev.cur(), env)
|
|
|
24 |
|
| 38051 |
ripley |
25 |
}
|
|
|
26 |
\arguments{
|
| 52081 |
murdoch |
27 |
\item{prompt}{prompt to be displayed to the user in the graphics window}
|
| 38051 |
ripley |
28 |
\item{onMouseDown}{a function to respond to mouse clicks}
|
|
|
29 |
\item{onMouseMove}{a function to respond to mouse movement}
|
|
|
30 |
\item{onMouseUp}{a function to respond to mouse button releases}
|
|
|
31 |
\item{onKeybd}{a function to respond to key presses}
|
| 52081 |
murdoch |
32 |
\item{consolePrompt}{prompt to be displayed to the user in the console}
|
|
|
33 |
\item{which}{which graphics device does the call apply to?}
|
|
|
34 |
\item{...}{items including handlers to be placed in the event environment}
|
|
|
35 |
\item{env}{an environment to be used as the event environment}
|
| 38051 |
ripley |
36 |
}
|
|
|
37 |
\details{
|
| 52081 |
murdoch |
38 |
These functions allow user input from some graphics devices (currently
|
| 61153 |
ripley |
39 |
only the \code{windows()} and \code{X11(type = "Xlib")} screen displays
|
| 52877 |
murdoch |
40 |
in base \R{}). Event handlers may be installed to respond to events
|
|
|
41 |
involving the mouse or keyboard.
|
| 38051 |
ripley |
42 |
|
| 52081 |
murdoch |
43 |
The functions are related as follows. If any of the first five
|
|
|
44 |
arguments to \code{getGraphicsEvent} are given, then it uses those
|
|
|
45 |
in a call to \code{setGraphicsEventHandlers} to replace any existing
|
|
|
46 |
handlers in the current device. This is for compatibility with pre-2.12.0 \R{}
|
| 61433 |
ripley |
47 |
versions. The current normal way to set up event handlers is to
|
| 52081 |
murdoch |
48 |
set them using \code{setGraphicsEventHandlers} or \code{setGraphicsEventEnv} on
|
|
|
49 |
one or more graphics devices, and then use \code{getGraphicsEvent()} with
|
|
|
50 |
no arguments to retrieve event data.
|
|
|
51 |
\code{getGraphicsEventEnv()} may be used to save the event environment
|
|
|
52 |
for use later.
|
|
|
53 |
|
|
|
54 |
The names of the arguments in \code{getGraphicsEvent} are special. When
|
| 61433 |
ripley |
55 |
handling events, the graphics system will look through the event
|
| 52081 |
murdoch |
56 |
environment for functions named \code{onMouseDown}, \code{onMouseMove}, \code{onMouseUp}
|
| 61433 |
ripley |
57 |
and \code{onKeybd} and use them as event handlers. It will use
|
| 52081 |
murdoch |
58 |
\code{prompt} for a label on the graphics device. Two other special names are
|
|
|
59 |
\code{which}, which will identify the graphics device, and
|
|
|
60 |
\code{result}, where the result of the last event
|
|
|
61 |
handler will be stored before being returned by \code{getGraphicsEvent()}.
|
|
|
62 |
|
| 61433 |
ripley |
63 |
The mouse event handlers should be functions with header
|
| 38051 |
ripley |
64 |
\code{function(buttons, x, y)}. The coordinates \code{x}
|
|
|
65 |
and \code{y} will be passed to mouse event handlers in device independent
|
| 61433 |
ripley |
66 |
coordinates (i.e. the lower left corner of the window is \code{(0,0)},
|
|
|
67 |
the upper right is \code{(1,1)}). The \code{buttons} argument
|
| 38051 |
ripley |
68 |
will be a vector listing the buttons
|
| 61433 |
ripley |
69 |
that are pressed at the time of the event, with 0 for left, 1 for middle, and 2
|
| 38051 |
ripley |
70 |
for right.
|
|
|
71 |
|
|
|
72 |
The keyboard event handler should be a function with header
|
|
|
73 |
\code{function(key)}. A single element character vector will be passed
|
|
|
74 |
to this handler, corresponding to the key press. Shift and other modifier
|
|
|
75 |
keys will have been processed, so \code{shift-a} will be passed as
|
|
|
76 |
\code{"A"}. The following special keys may also be passed to the handler:
|
|
|
77 |
\itemize{
|
|
|
78 |
\item Control keys, passed as \code{"Ctrl-A"}, etc.
|
| 61173 |
ripley |
79 |
\item Navigation keys, passed as one of\cr
|
|
|
80 |
\code{"Left", "Up", "Right", "Down", "PgUp", "PgDn", "End", "Home"}
|
| 38051 |
ripley |
81 |
\item Edit keys, passed as one of \code{"Ins", "Del"}
|
|
|
82 |
\item Function keys, passed as one of \code{"F1", "F2", ...}
|
|
|
83 |
}
|
|
|
84 |
|
| 52081 |
murdoch |
85 |
The event handlers are standard R functions, and will be executed as though called
|
| 61433 |
ripley |
86 |
from the event environment.
|
| 38051 |
ripley |
87 |
|
| 52221 |
murdoch |
88 |
In an interactive session, events will be processed until
|
| 38051 |
ripley |
89 |
\itemize{
|
|
|
90 |
\item one of the event handlers returns
|
|
|
91 |
a non-\code{NULL} value which will be returned as the value of
|
| 61433 |
ripley |
92 |
\code{getGraphicsEvent}, or
|
| 38051 |
ripley |
93 |
\item the user interrupts the function from the
|
|
|
94 |
console.
|
|
|
95 |
}
|
|
|
96 |
}
|
|
|
97 |
\value{
|
| 61433 |
ripley |
98 |
When run interactively,
|
| 52081 |
murdoch |
99 |
\code{getGraphicsEvent} returns a non-\code{NULL} value returned from one of the event handlers.
|
| 52221 |
murdoch |
100 |
In a non-interactive session, \code{getGraphicsEvent} will return \code{NULL} immediately.
|
| 52081 |
murdoch |
101 |
|
|
|
102 |
\code{getGraphicsEventEnv} returns the current event environment for the graphics device,
|
|
|
103 |
or \code{NULL} if none has been set.
|
|
|
104 |
|
|
|
105 |
\code{setGraphicsEventEnv} and \code{setGraphicsEventHandlers} return the previous
|
|
|
106 |
event environment for the graphics device.
|
| 38051 |
ripley |
107 |
}
|
|
|
108 |
\author{Duncan Murdoch}
|
|
|
109 |
\examples{
|
| 52081 |
murdoch |
110 |
\donttest{
|
| 61153 |
ripley |
111 |
savepar <- par(ask = FALSE)
|
|
|
112 |
dragplot <- function(..., xlim = NULL, ylim = NULL, xaxs = "r", yaxs = "r") {
|
|
|
113 |
plot(..., xlim = xlim, ylim = ylim, xaxs = xaxs, yaxs = yaxs)
|
| 52081 |
murdoch |
114 |
startx <- NULL
|
|
|
115 |
starty <- NULL
|
|
|
116 |
usr <- NULL
|
|
|
117 |
|
|
|
118 |
devset <- function()
|
|
|
119 |
if (dev.cur() != eventEnv$which) dev.set(eventEnv$which)
|
| 61433 |
ripley |
120 |
|
| 52081 |
murdoch |
121 |
dragmousedown <- function(buttons, x, y) {
|
|
|
122 |
startx <<- x
|
|
|
123 |
starty <<- y
|
|
|
124 |
devset()
|
|
|
125 |
usr <<- par("usr")
|
|
|
126 |
eventEnv$onMouseMove <- dragmousemove
|
|
|
127 |
NULL
|
| 38051 |
ripley |
128 |
}
|
| 61433 |
ripley |
129 |
|
| 52081 |
murdoch |
130 |
dragmousemove <- function(buttons, x, y) {
|
|
|
131 |
devset()
|
| 61168 |
ripley |
132 |
deltax <- diff(grconvertX(c(startx, x), "ndc", "user"))
|
|
|
133 |
deltay <- diff(grconvertY(c(starty, y), "ndc", "user"))
|
| 61153 |
ripley |
134 |
plot(..., xlim = usr[1:2]-deltax, xaxs = "i",
|
|
|
135 |
ylim = usr[3:4]-deltay, yaxs = "i")
|
| 47621 |
ripley |
136 |
NULL
|
| 38051 |
ripley |
137 |
}
|
| 61433 |
ripley |
138 |
|
|
|
139 |
mouseup <- function(buttons, x, y) {
|
| 52081 |
murdoch |
140 |
eventEnv$onMouseMove <- NULL
|
|
|
141 |
}
|
| 61433 |
ripley |
142 |
|
| 52081 |
murdoch |
143 |
keydown <- function(key) {
|
|
|
144 |
if (key == "q") return(invisible(1))
|
|
|
145 |
eventEnv$onMouseMove <- NULL
|
|
|
146 |
NULL
|
| 38051 |
ripley |
147 |
}
|
| 61433 |
ripley |
148 |
|
| 61153 |
ripley |
149 |
setGraphicsEventHandlers(prompt = "Click and drag, hit q to quit",
|
| 52081 |
murdoch |
150 |
onMouseDown = dragmousedown,
|
|
|
151 |
onMouseUp = mouseup,
|
|
|
152 |
onKeybd = keydown)
|
|
|
153 |
eventEnv <- getGraphicsEventEnv()
|
| 38051 |
ripley |
154 |
}
|
| 52081 |
murdoch |
155 |
|
|
|
156 |
dragplot(rnorm(1000), rnorm(1000))
|
| 56280 |
ripley |
157 |
# This currently only works on the Windows
|
|
|
158 |
# and X11(type = "Xlib") screen devices...
|
| 52081 |
murdoch |
159 |
getGraphicsEvent()
|
|
|
160 |
par(savepar)
|
| 38051 |
ripley |
161 |
}
|
| 52081 |
murdoch |
162 |
}
|
| 45849 |
ripley |
163 |
\keyword{iplot}
|