Answer the question
In order to leave comments, you need to log in
How to catch the user pressing the Esc button in the win console?
In general, there is a task, but one of the points is:
- abort the operation if the user pressed the Esc button
The implementation should work in the windows console, but I don't know how to implement it.
Can you tell me where to look for this issue?
PS GUI (swing / fx / etc) do not roll
Answer the question
In order to leave comments, you need to log in
This is not possible without using JNI. If the use of native calls is acceptable for you, then the first thing we need is the java code itself. Let's put it in ExitOnEscape.java :
public class ExitOnEscape implements Runnable {
private boolean doTheStuff = true; // Флаг активности рабочего потока
public native boolean readConsole();
public void run() { // Рабочий поток
while (doTheStuff) { // Работать пока установлен флаг
System.out.println("Doing something..."); // Основная логика программы
try {
Thread.sleep(500);
}
catch (InterruptedException exc) {}
}
}
public void doSomething() {
new Thread(this).start(); // Запускаем рабочий поток
while(true) { // В главном потоке читаем консоль нативной функцией в бесконечном цикле
if(readConsole()) { // Если нажат escape
doTheStuff = false; // Сбрасываем флаг активности
break; // И выходим из цикла
}
}
}
public static void main(String[] args) {
System.loadLibrary("ExitOnEscape");
ExitOnEscape exitOnEscape = new ExitOnEscape();
exitOnEscape.doSomething();
}
}
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class ExitOnEscape */
#ifndef _Included_ExitOnEscape
#define _Included_ExitOnEscape
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: ExitOnEscape
* Method: readConsole
* Signature: ()Z
*/
JNIEXPORT jboolean JNICALL Java_ExitOnEscape_readConsole
(JNIEnv *, jobject);
#ifdef __cplusplus
}
#endif
#endif
#include <stdio.h>
#include <conio.h>
#include "ExitOnEscape.h"
JNIEXPORT jboolean JNICALL Java_ExitOnEscape_readConsole(JNIEnv* env, jobject obj) {
char c = getch(); // Считываем введённый символ
if (c == 27) // Если это escape
return JNI_TRUE; // Возвращаем true
else
return JNI_FALSE; // Иначе false
}
gcc -Wl,--add-stdcall-alias ^
-I"%JAVA_HOME%\include" ^
-I"%JAVA_HOME%\include\win32" ^
-shared -o ExitOnEscape.dll ExitOnEscape.c
java ExitOnEscape
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question