The R Project SVN R

Rev

Rev 72261 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
42333 ripley 1
% File src/library/grDevices/man/getGraphicsEvent.Rd
68948 ripley 2
% Part of the R package, https://www.R-project.org
67599 ripley 3
% Copyright 1995-2014 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,
72261 murrell 20
                 onIdle = NULL,
52081 murdoch 21
                 consolePrompt = prompt)
22
setGraphicsEventHandlers(which = dev.cur(), ...)
23
getGraphicsEventEnv(which = dev.cur())
24
setGraphicsEventEnv(which = dev.cur(), env)
25
 
38051 ripley 26
}
27
\arguments{
52081 murdoch 28
  \item{prompt}{prompt to be displayed to the user in the graphics window}
38051 ripley 29
  \item{onMouseDown}{a function to respond to mouse clicks}
30
  \item{onMouseMove}{a function to respond to mouse movement}
31
  \item{onMouseUp}{a function to respond to mouse button releases}
32
  \item{onKeybd}{a function to respond to key presses}
72261 murrell 33
  \item{onIdle}{a function to call when no events are pending}
52081 murdoch 34
  \item{consolePrompt}{prompt to be displayed to the user in the console}
35
  \item{which}{which graphics device does the call apply to?}
36
  \item{...}{items including handlers to be placed in the event environment}
37
  \item{env}{an environment to be used as the event environment}
38051 ripley 38
}
39
\details{
52081 murdoch 40
These functions allow user input from some graphics devices (currently
72261 murrell 41
only the \code{windows()}, \code{X11(type = "Xlib")}
42
and \code{X11(type = "cairo")} screen displays
52877 murdoch 43
in base \R{}). Event handlers may be installed to respond to events
44
involving the mouse or keyboard.
38051 ripley 45
 
72261 murrell 46
The functions are related as follows.  If any of the first six
52081 murdoch 47
arguments to \code{getGraphicsEvent} are given, then it uses those
48
in a call to \code{setGraphicsEventHandlers} to replace any existing
49
handlers in the current device.  This is for compatibility with pre-2.12.0 \R{}
61433 ripley 50
versions.  The current normal way to set up event handlers is to
52081 murdoch 51
set them using \code{setGraphicsEventHandlers} or \code{setGraphicsEventEnv} on
52
one or more graphics devices, and then use \code{getGraphicsEvent()} with
53
no arguments to retrieve event data.
54
\code{getGraphicsEventEnv()} may be used to save the event environment
55
for use later.
56
 
57
The names of the arguments in \code{getGraphicsEvent} are special.  When
61433 ripley 58
handling events, the graphics system will look through the event
72261 murrell 59
environment for functions named \code{onMouseDown}, \code{onMouseMove},
60
\code{onMouseUp}, \code{onKeybd}, and \code{onIdle}, and use them as
61
event handlers.  It will use
52081 murdoch 62
\code{prompt} for a label on the graphics device.  Two other special names are
63
\code{which}, which will identify the graphics device, and
64
\code{result}, where the result of the last event
65
handler will be stored before being returned by \code{getGraphicsEvent()}.
66
 
61433 ripley 67
The mouse event handlers should be functions with header
38051 ripley 68
\code{function(buttons, x, y)}.  The coordinates \code{x}
69
and \code{y} will be passed to mouse event handlers in device independent
66444 hornik 70
coordinates (i.e., the lower left corner of the window is \code{(0,0)},
61433 ripley 71
the upper right is \code{(1,1)}).  The \code{buttons} argument
38051 ripley 72
will be a vector listing the buttons
61433 ripley 73
that are pressed at the time of the event, with 0 for left, 1 for middle, and 2
38051 ripley 74
for right.
75
 
76
The keyboard event handler should be a function with header
77
\code{function(key)}.  A single element character vector will be passed
78
to this handler, corresponding to the key press.  Shift and other modifier
79
keys will have been processed, so \code{shift-a} will be passed as
80
\code{"A"}.  The following special keys may also be passed to the handler:
81
\itemize{
82
     \item Control keys, passed as \code{"Ctrl-A"}, etc.
61173 ripley 83
     \item Navigation keys, passed as one of\cr
84
     \code{"Left", "Up", "Right", "Down", "PgUp", "PgDn", "End", "Home"}
38051 ripley 85
     \item Edit keys, passed as one of \code{"Ins", "Del"}
86
     \item Function keys, passed as one of \code{"F1", "F2", ...}
87
}
88
 
72261 murrell 89
The idle event handler \code{onIdle} should be a function with no
90
arguments. If the function is undefined or \code{NULL}, then R will
91
typically call a system function which (efficiently) waits for the next
85925 hornik 92
event to appear on a file handle. Otherwise, the idle event handler will
72261 murrell 93
be called whenever the event queue of the graphics device was found to
94
be empty, i.e. in an infinite loop. This feature is intended to allow
95
animations to respond to user input, and could be CPU-intensive.
96
Currently, \code{onIdle} is only implemented for \code{X11()} devices.
97
 
98
Note that calling \code{Sys.sleep()} is not recommended within an idle
99
handler - \code{Sys.sleep()} removes pending graphics events in order to
100
allow users to move, close, or resize windows while it is executing.
101
Events such as mouse and keyboard events occurring during
102
\code{Sys.sleep()} are lost, and currently do not trigger the event
103
handlers registered via \code{getGraphicsEvent} or
104
\code{setGraphicsEventHandlers}.
105
 
106
 
52081 murdoch 107
The event handlers are standard R functions, and will be executed as though called
61433 ripley 108
from the event environment.
38051 ripley 109
 
52221 murdoch 110
In an interactive session, events will be processed until
38051 ripley 111
\itemize{
112
     \item one of the event handlers returns
113
a non-\code{NULL} value which will be returned as the value of
61433 ripley 114
\code{getGraphicsEvent}, or
38051 ripley 115
      \item the user interrupts the function from the
116
console.
117
}
118
}
119
\value{
61433 ripley 120
When run interactively,
52081 murdoch 121
\code{getGraphicsEvent} returns a non-\code{NULL} value returned from one of the event handlers.
64077 murdoch 122
In a non-interactive session, \code{getGraphicsEvent} will return \code{NULL} immediately.  It
123
will also return \code{NULL} if the user closes the last window that has graphics handlers.
52081 murdoch 124
 
125
\code{getGraphicsEventEnv} returns the current event environment for the graphics device,
126
or \code{NULL} if none has been set.
127
 
128
\code{setGraphicsEventEnv} and \code{setGraphicsEventHandlers} return the previous
129
event environment for the graphics device.
38051 ripley 130
}
131
\author{Duncan Murdoch}
132
\examples{
72261 murrell 133
# This currently only works on the Windows, X11(type = "Xlib"), and
134
# X11(type = "cairo") screen devices...
65698 ripley 135
\dontrun{
61153 ripley 136
savepar <- par(ask = FALSE)
137
dragplot <- function(..., xlim = NULL, ylim = NULL, xaxs = "r", yaxs = "r") {
138
    plot(..., xlim = xlim, ylim = ylim, xaxs = xaxs, yaxs = yaxs)
52081 murdoch 139
    startx <- NULL
140
    starty <- NULL
64106 murdoch 141
    prevx <- NULL
142
    prevy <- NULL
52081 murdoch 143
    usr <- NULL
144
 
145
    devset <- function()
146
        if (dev.cur() != eventEnv$which) dev.set(eventEnv$which)
61433 ripley 147
 
52081 murdoch 148
    dragmousedown <- function(buttons, x, y) {
149
        startx <<- x
150
        starty <<- y
64106 murdoch 151
        prevx <<- 0
152
        prevy <<- 0
52081 murdoch 153
        devset()
154
        usr <<- par("usr")
155
        eventEnv$onMouseMove <- dragmousemove
156
        NULL
38051 ripley 157
    }
61433 ripley 158
 
52081 murdoch 159
    dragmousemove <- function(buttons, x, y) {
160
        devset()
61168 ripley 161
        deltax <- diff(grconvertX(c(startx, x), "ndc", "user"))
162
        deltay <- diff(grconvertY(c(starty, y), "ndc", "user"))
64106 murdoch 163
	if (abs(deltax-prevx) + abs(deltay-prevy) > 0) {
164
	    plot(..., xlim = usr[1:2]-deltax, xaxs = "i",
165
		      ylim = usr[3:4]-deltay, yaxs = "i")
166
	    prevx <<- deltax
167
	    prevy <<- deltay
168
	}
47621 ripley 169
        NULL
38051 ripley 170
    }
61433 ripley 171
 
172
    mouseup <- function(buttons, x, y) {
52081 murdoch 173
    	eventEnv$onMouseMove <- NULL
174
    }	
61433 ripley 175
 
52081 murdoch 176
    keydown <- function(key) {
177
        if (key == "q") return(invisible(1))
178
        eventEnv$onMouseMove <- NULL
179
        NULL
38051 ripley 180
    }
61433 ripley 181
 
61153 ripley 182
    setGraphicsEventHandlers(prompt = "Click and drag, hit q to quit",
52081 murdoch 183
                     onMouseDown = dragmousedown,
184
                     onMouseUp = mouseup,
185
                     onKeybd = keydown)
186
    eventEnv <- getGraphicsEventEnv()
38051 ripley 187
}
52081 murdoch 188
 
189
dragplot(rnorm(1000), rnorm(1000))
190
getGraphicsEvent()
191
par(savepar)
38051 ripley 192
}
52081 murdoch 193
}
45849 ripley 194
\keyword{iplot}