R键按下而不阻止/事件处理

时间:2018-02-14 17:08:13

标签: r events keyboard event-handling

我很难找到我的R代码的解决方案来检查按键的状态而不会阻止执行。这是一些非常简单的演示"动画"。

我在RStudio 1.0.136,Mac OS X 10.13.3,R 3.3.3。

我试过"按键"来自Cran的库,但keypress_support()返回false和

key<-keypress(block = FALSE)

返回错误

  

&#34;您的平台/终端不支持按键&#34;。

我尝试在x11图形窗口中使用事件,但显然我误解了一些东西,因为当我使用getGraphicsEvent()时,从不调用idle函数:

#Please ignore the nasty global varables, quick eample!
# When q is pressed return non-null to exit
keydown <- function(key) {
  if (key == "q") return(invisible(1))
  NULL
}

#Plots a new blob in the idle state!
pants <- function() {
  points(x[i], y[i], pch = 19, cex = 2,col="red")
  i<<-i+1
  NULL
}

#Start here
x11(width = 10, height = 3)

n = 2000
x = sort(rnorm(n))
y = rnorm(n)
i=0

plot.new()
plot.window(c(1, n), c(0, 1))

setGraphicsEventHandlers(prompt = "Click q to quit",
                         onMouseDown = NULL,
                         onMouseUp = NULL,
                         onIdle=pants,
                         onKeybd = keydown)

eventEnv <- getGraphicsEventEnv()

plot(x, y, type = "n")

# I assumed this would continue to call the idle process until 
# a non-idle event is triggers, but it never gets called?
getGraphicsEvent()
print("end")

我正在尝试设计在(连续)循环中绘制点的代码,并且当检测到键盘事件时调用函数进行响应。

0 个答案:

没有答案
相关问题