How can I make a clojure program block until a key is pressed?
I want the equivalent behaviour of a blocking read from stdin in other languages.
The easiest way is to use ConsoleReader from JLine.
See the Clojure Cookbook for details: https://github.com/clojure-cookbook/clojure-cookbook/blob/master/04_local-io/4-02_read-unbuffered-keystroke.asciidoc
The question in JohnJ's comment (Reading unbuffered keyboard input in Clojure) gives a very similar example to the one in the Cookbook.
Related
Let's say I want to call a C++ program from Clojure, like stockfish.
If I execute stockfish from the terminal, it stays open and interactive until the command is quit.
However, if I call from Clojure, it just calls it once and closes it.
I have used the programs macro of the me/conch package, like this:
user> (programs stockfish)
user> (stockfish "uci")
"Stockfish 030620 64 by T. Romstad ... \nuciok\n"
And the program stops. How can I get the process to stay open and to keep interacting until I tell it to quit?
This sort of problem has a canonical solution in the Unix expect tool, which has been replicated in, for example, Perl's Expect module. There might also be a Java version of Expect. If so, it might be a more direct solution.
About conch specifically, the README.md at https://github.com/Raynes/conch presents two ways of invoking a program. The first way is easy, but sends only one burst of standard input before closing the program, as you observed. The second way will be harder to work with, but you can send more input whenever you want by writing to the process' standard-input, and recover output whenever you want by reading from its standard-output. It's under the heading https://github.com/Raynes/conch#low-level-usage. It looks like a thin wrapper around the Java process API, which you might as well use directly.
Im writing a little C++ progam but got to a point from which on i could not go on:
I want my program to control an external program using a certain combination of keys.
Yes, this sounds like a horribly unclean workaround, but thats what i want to do ;)
To be more specific:
I want my little C++ program to control the already running program elinks (a text based internet browser) by pressing the "esc" then "v" and then "h" -keys (this is used to toggle between html and plain text output in elinks).
But unfortunately, I do not know how to get a C++ program to type a certain combination of keys (...if this is possible at all).
I already tried the search function but wasnt able to find a solution.
It would be very kind of you, if someone could give me a hint on what to do here.
Thanks,
Spoekenkieker
P.S.: Im running Linux
If you have control over how to start the elinks browser, you can start it under the GNU screen terminal multiplexer and use its stuff command to send key inputs:
https://unix.stackexchange.com/questions/13953/sending-text-input-to-a-detached-screen
This way you're not required to learn the complications of X Windows programming.
i want to do a simple program that will run another program (easy task in C++), hook the process and, when the program is closed, run another program (needed to sync a file modified by the first program).
The second program prompt out a popout asking a simple Yes/No buttons.
There is any way?
I am writing an article about hooking : http://ntvalk.blogspot.nl/2013/11/hooking-explained-detouring-library.html
It describes the probably most common methods for hooking under Windows. (jmp/vtable/etc)
gldraphael already described a method of running another program.
Check this out: http://faq.cprogramming.com/cgi-bin/smartfaq.cgi?answer=1044654269&id=1043284392
It lists the various ways you can run another program.
I need to block alt+shift keys event using C++, or some way to block changing language.
Thanks in advance.
I would go about it a bit differently. I would catch current langauge settings at startup than change it for desired one.
At the even of alt+shift i would just set it back to the desired type again.
It should be fairy easy to do with net framework.
Here is a short article about manipulating languages: How to change input language programmatically
And main class on msdna: InputLanguage documentation
To actually prevent alt+shift from moving into windows system you would have to play with hooks.
Here is an article about blocking keystrokes before windows process them.
Just an idea. You can catch WM_KEYDOWN message and call ActivateKeyboardLayout to switch language.
Using C++ you can install a keyboard hook procedure like the one suggested here and filter (swallow/don't propagate) the key(s) you want to forbid.
My understanding of MSDN is that you can pretend to process WM_INPUTLANGCHANGEREQUEST and then do nothing, so that Windows will not do anything further and the language will not actually change. But some users say that doesn't work any more.
http://msdn.microsoft.com/en-us/library/ms632630(VS.85).aspx
Maybe you can implement ITfInputProcessorProfileActivationSink::OnActivated, and when you get called you can change back to the previous language by calling ITfInputProcessorProfiles::ActivateLanguageProfile. At the beginning of your app you would call ITfInputProcessorProfiles::GetActiveLanguageProfile.
Maybe you can implement ITfLanguageProfileNotifySink::OnLanguageChange, set *pfAccept
to FALSE and return S_OK.
http://msdn.microsoft.com/en-us/library/windows/desktop/ms628770(v=vs.85).aspx
All of the above have a problem. If the user intentionally changes languages on the client, for example by clicking on the client's task bar instead of pressing Alt+Shift, the above methods will prevent their change anyway.
I wonder if RegisterHotKey would let you register Alt+Shift for your own window even though the system already had it defined.
The thing you are trying to implement is keyboard hook. The detailed explanation with source code in C/C++ can be found here:
http://www.codeproject.com/Articles/67091/Mouse-and-KeyBoard-Hooking-utility-with-VC
Also other helpful examples can be found here:
http://www.codeproject.com/Articles/1264/KeyBoard-Hooks
http://www.codeproject.com/Articles/9513/Disable-keyboard-and-show-images-for-the-children
Hope this helps.
Kind regards,
Bo
I'm currently developing various console games in Windows that won't really work using regular input via cin.
How can I (In a simple way using only standard windows libraries available in MSVC):
Make the program wait for a (specific?) key press and return the key ID (It would have to work for all keys including the arrow keys)
During a real-time game check for the last pressed key of the user and if there was any key pressed since the last check.
It would really help if you could include a short example program for your solution
I've got just what you need.
Here enjoy pal:
C++ source
It's pretty much self-explanatory but if you have any doubts my email is jacobossm#gmail.com
AFAIK you can't do it using the standard C runtime. You will need to use something such as the Win32 function GetAsyncKeyState.
You want the Windows Console API, for example PeekConsoleInput.