Read key in Eiffel console application - console

Is there anything like Console.ReadKey from .NET in Eiffel on Windows?
I need a way to read input from the console without waiting for the user to press Enter.
The function io.read_character cannot be used because it blocks until the user presses Enter.

As explained in answers on SO (here or here) as well as elsewhere, there is no portable way to read a character from a console without waiting. However you can easily use any of the approaches listed in the links by interfacing to external code from Eiffel. The following example demonstrates how to do it on Windows:
read_char: CHARACTER
-- Read a character from a console without waiting for Enter.
external "C inline use <conio.h>"
alias "return getch ();"
end
Then the feature read_char can be called from your code as a regular one:
from
io.put_string ("Press q or Esc to exit.")
io.put_new_line
until
c = 'q' or c = '%/27/'
loop
c := read_char
io.put_string ("You pressed: ")
if c = '%U' or c = '%/224/' then
-- Extended key is pressed, read next character.
c := read_char
io.put_string ("extended key ")
io.put_natural_32 (c.natural_32_code)
else
io.put_character (c)
end
io.put_new_line
end

Related

Fortran90: Scripting of Standard In not working as expected

Working with Fortran90 in Unix...
I have a programme which needs to read in the input parameters from a file "input-deck.par". This filename is currently hard-coded but I want to run a number of runs using different input-deck files (input-deck01.par, input-deck02.par, input-deck03.par etc.) so I've set-up the code to do a simple "read(*,*) inpfile" to allow the user to input the name of this file directly on run-time with a view to scripting this later.
This works fine interactively. If I execute the programme it asks for the file name, you type it in and the filename is accepted, the file is opened and the programme picks up the parameters from that file.
The issue lies in scripting this. I've tried scripting using the "<" pipe command so:
myprog.e < input-deck01.par
But I get an error saying:
Fortran runtime error: Cannot open file '----------------': No such file or directory
If I print the filename right after the input line, it prints that the filename is '----------------' (I initialise the variable as having 16 characters hence the 16 hyphens I think)
It seems like the "<" pipe is not passing the keyboard input in correctly. I've tried with spaces and quotes around the filename (various combinations) but the errors are the same.
Can anyone help?
(Please be gentle- this is my first post on SO and Fortran is not my primary language....)
Fortran has the ability to read the command line arguments. A simple example is
program foo
implicit none
character(len=80) name
logical available
integer fd
if (command_argument_count() == 1) then
call get_command_argument(1, name)
else
call usage
end if
inquire(file=name, exist=available)
if (.not. available) then
call usage
end if
open(newunit=fd, file=name, status='old')
! Read file
contains
subroutine usage
write(*,'(A)') 'Usage: foo filename'
write(*,'(A)') ' filename --> file containing input info'
stop
end subroutine usage
end program foo
Instead of piping the file into the executable you simply do
% foo input.txt

Test if a key is pressed in Julia

I would like to start a julia script by using the keyboard shortcut Ctrl+Shift+H.
When the script starts, the keys Ctrl and Shift may still be pressed (depending on how quickly I remove my fingers).
I would like to be sure that these two keys are released before executing the rest of the script. To do that I need to test if the key are pressed.
Note that triggering an event when the key are released would not be enough as the keys may not be pressed when the script starts.
I found several references to detect when a key is pressed in Julia but I did not find one to test if a key is currently pressed.
Do you know how I could do that?
Here is a minimal example of why I would like to do that.
You can find here a file in the cnee format which enables to write "azerty" in the window which has the focus. If you type the following command in a terminal, "azerty" is typed in it as expected:
cnee --replay -sp 0 -f ./recorded_macro.xnl
In order to write "azerty" in any window (not just terminals), I create a keyboard shortcut Ctrl+Shift+H which executes this command. If I use the shortcut, the keys Ctrl and Shift are likely to be pressed when the command is executed (except if I use the shortcut very quickly) and instead of typing "azerty", the window with the focus will get Ctrl+Shift+a, Ctrl+Shift+z, Ctrl+Shift+e, Ctrl+Shift+r, Ctrl+Shift+t and Ctrl+Shift+y which will trigger actions in the window but will not write "azerty".
So, I would like instead to start a python script with this shortcut which waits for Ctrl and Shift to be non-pressed before executing the cnee command. Note again that waiting for the keys to be released with a listener is not a solution as the keys Ctrl and Shift may not be pressed at the start of the python script if I use the shortcut quickly (and so I will have to press again Ctrl and Shift before executing the cnee command which I do not want).
The following Gtk program will detect the release of shift, control, and alt that are already pressed before the program starts or gets the focus. Note that my keyboard at least seems to not always detect multiple keystrokes, so perhaps depending on your keyboard you may have to just detect one of those releases.
using Gtk
function keypresswindow()
txt = "Press and Release a Key"
state = ""
win = GtkWindow("Key Release Test", 500, 30) |> (GtkFrame() |> ((vbox = GtkBox(:v)) |> (lab = GtkLabel(txt))))
function keyreleasecall(w, event)
event.keyval == 65505 && (state *= "SHIFT ")
event.keyval == 65507 && (state *= "CONTROL ")
event.keyval == 65513 && (state *= "ALT ")
set_gtk_property!(lab, :label, "You have released: $state")
end
signal_connect(keyreleasecall, win, "key-release-event")
cond = Condition()
endit(w) = notify(cond)
signal_connect(endit, win, :destroy)
showall(win)
wait(cond)
end
keypresswindow()

AutoIt Scripting for an External CLI Program - eac3to.exe

I am attempting to design a front end GUI for a CLI program by the name of eac3to.exe. The problem as I see it is that this program sends all of it's output to a cmd window. This is giving me no end of trouble because I need to get a lot of this output into a GUI window. This sounds easy enough, but I am begining to wonder whether I have found one of AutoIt's limitations?
I can use the Run() function with a windows internal command such as Dir and then get the output into a variable with the AutoIt StdoutRead() function, but I just can't get the output from an external program such as eac3to.exe - it just doesn't seem to work whatever I do! Just for testing purposesI I don't even need to get the output to a a GUI window: just printing it with ConsoleWrite() is good enough as this proves that I was able to read it into a variable. So at this stage that's all I need to do - get the text (usually about 10 lines) that has been output to a cmd window by my external CLI program into a variable. Once I can do this the rest will be a lot easier. This is what I have been trying, but it never works:
Global $iPID = Run("C:\VIDEO_EDITING\eac3to\eac3to.exe","", #SW_SHOW)
Global $ScreenOutput = StdoutRead($iPID)
ConsoleWrite($ScreenOutput & #CRLF)
After running this script all I get from the consolWrite() is a blank line - not the text data that was output as a result of running eac3to.exe (running eac3to without any arguments just lists a screen of help text relating to all the commandline options), and that's what I am trying to get into a variable so that I can put it to use later in the program.
Before I suggest a solution let me just tell you that Autoit has one
of the best help files out there. Use it.
You are missing $STDOUT_CHILD = Provide a handle to the child's STDOUT stream.
Also, you can't just do RUN and immediately call stdoutRead. At what point did you give the app some time to do anything and actually print something back to the console?
You need to either use ProcessWaitClose and read the stream then or, you should read the stream in a loop. Simplest check would be to set a sleep between RUN and READ and see what happens.
#include <AutoItConstants.au3>
Global $iPID = Run("C:\VIDEO_EDITING\eac3to\eac3to.exe","", #SW_SHOW, $STDOUT_CHILD)
; Wait until the process has closed using the PID returned by Run.
ProcessWaitClose($iPID)
; Read the Stdout stream of the PID returned by Run. This can also be done in a while loop. Look at the example for StderrRead.
; If the proccess doesnt end when finished you need to put this inside of a loop.
Local $ScreenOutput = StdoutRead($iPID)
ConsoleWrite($ScreenOutput & #CRLF)

User input when executing R code in batch mode

I am searching for a way to get user input inside a loop while executing in batch mode.
readLines() and scan() work well for me in interactive mode only, in batch mode they start to read in lines of code as user input, unless all the code is surrounded by {}, which is inconvenient. I need a simple solution to get just 1 integer value in a way that I can just type in value and press ENTER, so
the input field (if the solution involves GUI) must automatically get focus and
ENTER must trigger end of input/submission.
I can't find a way to do it that will satisfy both conditions, e.g. ginput() from gWidgets activates the input field, but ENTER doesn't trigger form submission.
Here is how I solved my own problem:
require(gWidgets)
options(guiToolkit="RGtk2")
INPUT <- function(message) {
CHOICE <- NA
w <- gbasicdialog(title=message, handler = function(h,...) CHOICE <<- svalue(input))
input <- gedit("", initial.msg="", cont=w, width=10)
addHandlerChanged(input, handler=function (h, ...) {
CHOICE <<- svalue(input)
dispose(w)
})
visible(w, set=TRUE)
return(CHOICE)
}
repeat{
x=INPUT("Input an integer")
if(!is.na(as.integer(x))) break
}
print(x)
Update:
I can't test this right now, but take a look at ?menu and have it pop up a gui window.
I'm not certain if that will work, but it is different in that it takes a mouse-click response.
original answer:
As per the documentation to ?readline:
This can only be used in an interactive session.
..
In non-interactive use the result is as if the response was RETURN and the value is "".
If you are simply waiting for one piece of information, and you do not know this piece of information before beginning the execution of the script (presumably, there is a decision to be made which is dependent on the results earlier in the script), then one alternative is to simply break your script up into three parts:
everything before the decision point.
an interactive script which prompts for input
everything after the decision point.
And simply chain the three together by having the first end by calling the second in an interactive session. Then have the second end by calling the third.

Assigning unlisted shortcut keys / key binding

As one of the millions of people around the world I do not have an "anglo-saxon" keyboard and frustratingly I don't have {} on my keyboard (i have è ° à and other useful stuff for italian). How hassly for programming is that!!!!!
So I'd like to assign a key binding to curly brackets but they are not listed in the preferences->general->keys.
Is it possible? Have I missed it?
To confirm, you'd like to press one key and instead get a curly brace? You could so that using commands. The example below approximates what the editor would normally do by inserting a {} and placing the cursor in the middle.
1) Create a new Ruble via File > New Ruby project > Go to next page for "templates" and select a Ruble template
2) Edit the commands/commands.rb file and insert the following (adjusting as appropriate)
require 'ruble'
command 'Insert Right Brace' do |cmd|
cmd.key_binding = 'q' # change this for a new key binding
cmd.scope = 'source'
cmd.output = :insert_as_snippet
cmd.input = :selection
cmd.invoke do |context|
print '{${0}}' # snippet to surround the cursor with braces
end
end

Resources