The R Project SVN R

Rev

Rev 61173 | Rev 64106 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 61173 Rev 61433
Line 12... Line 12...
12
\description{
12
\description{
13
This function waits for input from a graphics window in the
13
This function waits for input from a graphics window in the
14
form of a mouse or keyboard event.
14
form of a mouse or keyboard event.
15
}
15
}
16
\usage{
16
\usage{
17
getGraphicsEvent(prompt = "Waiting for input", 
17
getGraphicsEvent(prompt = "Waiting for input",
18
                 onMouseDown = NULL, onMouseMove = NULL,
18
                 onMouseDown = NULL, onMouseMove = NULL,
19
                 onMouseUp = NULL, onKeybd = NULL,
19
                 onMouseUp = NULL, onKeybd = NULL,
20
                 consolePrompt = prompt)
20
                 consolePrompt = prompt)
21
setGraphicsEventHandlers(which = dev.cur(), ...)
21
setGraphicsEventHandlers(which = dev.cur(), ...)
22
getGraphicsEventEnv(which = dev.cur())
22
getGraphicsEventEnv(which = dev.cur())
Line 42... Line 42...
42
 
42
 
43
The functions are related as follows.  If any of the first five
43
The functions are related as follows.  If any of the first five
44
arguments to \code{getGraphicsEvent} are given, then it uses those
44
arguments to \code{getGraphicsEvent} are given, then it uses those
45
in a call to \code{setGraphicsEventHandlers} to replace any existing
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{}
46
handlers in the current device.  This is for compatibility with pre-2.12.0 \R{}
47
versions.  The current normal way to set up event handlers is to 
47
versions.  The current normal way to set up event handlers is to
48
set them using \code{setGraphicsEventHandlers} or \code{setGraphicsEventEnv} on
48
set them using \code{setGraphicsEventHandlers} or \code{setGraphicsEventEnv} on
49
one or more graphics devices, and then use \code{getGraphicsEvent()} with
49
one or more graphics devices, and then use \code{getGraphicsEvent()} with
50
no arguments to retrieve event data.
50
no arguments to retrieve event data.
51
\code{getGraphicsEventEnv()} may be used to save the event environment
51
\code{getGraphicsEventEnv()} may be used to save the event environment
52
for use later.
52
for use later.
53
 
53
 
54
The names of the arguments in \code{getGraphicsEvent} are special.  When
54
The names of the arguments in \code{getGraphicsEvent} are special.  When
55
handling events, the graphics system will look through the event 
55
handling events, the graphics system will look through the event
56
environment for functions named \code{onMouseDown}, \code{onMouseMove}, \code{onMouseUp}
56
environment for functions named \code{onMouseDown}, \code{onMouseMove}, \code{onMouseUp}
57
and \code{onKeybd} and use them as event handlers.  It will use 
57
and \code{onKeybd} and use them as event handlers.  It will use
58
\code{prompt} for a label on the graphics device.  Two other special names are
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
59
\code{which}, which will identify the graphics device, and
60
\code{result}, where the result of the last event
60
\code{result}, where the result of the last event
61
handler will be stored before being returned by \code{getGraphicsEvent()}.
61
handler will be stored before being returned by \code{getGraphicsEvent()}.
62
 
62
 
63
The mouse event handlers should be functions with header 
63
The mouse event handlers should be functions with header
64
\code{function(buttons, x, y)}.  The coordinates \code{x}
64
\code{function(buttons, x, y)}.  The coordinates \code{x}
65
and \code{y} will be passed to mouse event handlers in device independent
65
and \code{y} will be passed to mouse event handlers in device independent
66
coordinates (i.e. the lower left corner of the window is \code{(0,0)}, 
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 
67
the upper right is \code{(1,1)}).  The \code{buttons} argument
68
will be a vector listing the buttons
68
will be a vector listing the buttons
69
that are pressed at the time of the event, with 0 for left, 1 for middle, and 2 
69
that are pressed at the time of the event, with 0 for left, 1 for middle, and 2
70
for right.
70
for right.
71
 
71
 
72
The keyboard event handler should be a function with header
72
The keyboard event handler should be a function with header
73
\code{function(key)}.  A single element character vector will be passed
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
74
to this handler, corresponding to the key press.  Shift and other modifier
Line 81... Line 81...
81
     \item Edit keys, passed as one of \code{"Ins", "Del"}
81
     \item Edit keys, passed as one of \code{"Ins", "Del"}
82
     \item Function keys, passed as one of \code{"F1", "F2", ...}
82
     \item Function keys, passed as one of \code{"F1", "F2", ...}
83
}
83
}
84
 
84
 
85
The event handlers are standard R functions, and will be executed as though called
85
The event handlers are standard R functions, and will be executed as though called
86
from the event environment.  
86
from the event environment.
87
 
87
 
88
In an interactive session, events will be processed until
88
In an interactive session, events will be processed until
89
\itemize{
89
\itemize{
90
     \item one of the event handlers returns
90
     \item one of the event handlers returns
91
a non-\code{NULL} value which will be returned as the value of
91
a non-\code{NULL} value which will be returned as the value of
92
\code{getGraphicsEvent}, or 
92
\code{getGraphicsEvent}, or
93
      \item the user interrupts the function from the
93
      \item the user interrupts the function from the
94
console.
94
console.
95
}
95
}
96
}
96
}
97
\value{
97
\value{
98
When run interactively, 
98
When run interactively,
99
\code{getGraphicsEvent} returns a non-\code{NULL} value returned from one of the event handlers.
99
\code{getGraphicsEvent} returns a non-\code{NULL} value returned from one of the event handlers.
100
In a non-interactive session, \code{getGraphicsEvent} will return \code{NULL} immediately.
100
In a non-interactive session, \code{getGraphicsEvent} will return \code{NULL} immediately.
101
 
101
 
102
\code{getGraphicsEventEnv} returns the current event environment for the graphics device,
102
\code{getGraphicsEventEnv} returns the current event environment for the graphics device,
103
or \code{NULL} if none has been set.
103
or \code{NULL} if none has been set.
Line 115... Line 115...
115
    starty <- NULL
115
    starty <- NULL
116
    usr <- NULL
116
    usr <- NULL
117
 
117
 
118
    devset <- function()
118
    devset <- function()
119
        if (dev.cur() != eventEnv$which) dev.set(eventEnv$which)
119
        if (dev.cur() != eventEnv$which) dev.set(eventEnv$which)
120
        
120
 
121
    dragmousedown <- function(buttons, x, y) {
121
    dragmousedown <- function(buttons, x, y) {
122
        startx <<- x
122
        startx <<- x
123
        starty <<- y
123
        starty <<- y
124
        devset()
124
        devset()
125
        usr <<- par("usr")
125
        usr <<- par("usr")
126
        eventEnv$onMouseMove <- dragmousemove
126
        eventEnv$onMouseMove <- dragmousemove
127
        NULL
127
        NULL
128
    }
128
    }
129
    
129
 
130
    dragmousemove <- function(buttons, x, y) {
130
    dragmousemove <- function(buttons, x, y) {
131
        devset()
131
        devset()
132
        deltax <- diff(grconvertX(c(startx, x), "ndc", "user"))
132
        deltax <- diff(grconvertX(c(startx, x), "ndc", "user"))
133
        deltay <- diff(grconvertY(c(starty, y), "ndc", "user"))
133
        deltay <- diff(grconvertY(c(starty, y), "ndc", "user"))
134
        plot(..., xlim = usr[1:2]-deltax, xaxs = "i",
134
        plot(..., xlim = usr[1:2]-deltax, xaxs = "i",
135
                  ylim = usr[3:4]-deltay, yaxs = "i")
135
                  ylim = usr[3:4]-deltay, yaxs = "i")
136
        NULL
136
        NULL
137
    }
137
    }
138
    
138
 
139
    mouseup <- function(buttons, x, y) {    
139
    mouseup <- function(buttons, x, y) {
140
    	eventEnv$onMouseMove <- NULL
140
    	eventEnv$onMouseMove <- NULL
141
    }	
141
    }	
142
        
142
 
143
    keydown <- function(key) {
143
    keydown <- function(key) {
144
        if (key == "q") return(invisible(1))
144
        if (key == "q") return(invisible(1))
145
        eventEnv$onMouseMove <- NULL
145
        eventEnv$onMouseMove <- NULL
146
        NULL
146
        NULL
147
    }
147
    }
148
    
148
 
149
    setGraphicsEventHandlers(prompt = "Click and drag, hit q to quit",
149
    setGraphicsEventHandlers(prompt = "Click and drag, hit q to quit",
150
                     onMouseDown = dragmousedown,
150
                     onMouseDown = dragmousedown,
151
                     onMouseUp = mouseup,
151
                     onMouseUp = mouseup,
152
                     onKeybd = keydown)
152
                     onKeybd = keydown)
153
    eventEnv <- getGraphicsEventEnv()
153
    eventEnv <- getGraphicsEventEnv()