3

I have written a source code for my users. At the start of the code it prompts users' inputs.

The problem is, whenever they source the code, the focus is still in the source editor, such that some of them ended up typing their inputs in the source without noticing that. When they are aware of themselves not typing anything in the console, they move the focus themselves, without being aware of that they have already changed the source, and somehow saved it later unintentionally. The next time they run it, there are errors due to what they did to the source codes.

I know cat("\014") works like Ctrl+L to clear the console.

Though, what is the command that works like Ctrl+2 which is the shortcut that moves the focus to the console? In fact, is there a way to simulate specified key press in R?

Matthew Hui
  • 634
  • 5
  • 18

1 Answers1

2

I've never been able to find a program command to move the focus to the console during execution. One alternative is to change the settings in Tools > Global Options > Code and select "Focus console after executing from source". This will by default move the focus from the editor to the console after running code.

Otherwise key presses can be simulated using the KeyboardSimulator package.

install.packages("KeyboardSimulator")
keybd.press('Ctrl+2')
Andrew Eaves
  • 168
  • 12