Skip to content

Commit a6d7e0d

Browse files
authored
Merge pull request #560 from hx2A/fix549
Fix #549
2 parents 1a43a8e + 82128f5 commit a6d7e0d

File tree

5 files changed

+54
-4
lines changed

5 files changed

+54
-4
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
@@ meta
2+
name = intercept_escape()
3+
type = method
4+
pclass = Sketch
5+
category = structure
6+
subcategory = None
7+
8+
@@ signatures
9+
intercept_escape() -> None
10+
11+
@@ description
12+
Prevent the Escape key from causing the Sketch to exit. Normally hitting the Escape key (`ESC`) will cause the Sketch to exit. In Processing, one can write code to change the Escape key's behavior by changing the [](sketch_key) value to something else, perhaps with code similar to `py5.key = 'x'`. That code won't work in py5 because py5 does not allow the user to alter the value of [](sketch_key) like Processing does. The `intercept_escape()` method was created to allow users to achieve the same goal of preventing the Escape key from causing the Sketch to exit.
13+
14+
The `intercept_escape()` method will only do something when [](sketch_key) already equals `ESC`. This function should only be called from the user event functions `key_pressed()`, `key_typed()`, and `key_released()`.
15+
16+
This method will not alter the value of [](sketch_key). This method cannot prevent a Sketch from exiting when the exit is triggered by any other means, such as a call to [](sketch_exit_sketch) or the user closes the window.
17+
18+
@@ example
19+
def setup():
20+
py5.size(200, 200)
21+
22+
def draw():
23+
py5.rect(py5.mouse_x, py5.mouse_y, 10, 10)
24+
25+
def key_pressed():
26+
if py5.key == py5.ESC:
27+
# this code will not work:
28+
# py5.key = 'x'
29+
30+
py5.intercept_escape()
31+
32+
# verify py5.key has not changed
33+
assert py5.key == py5.ESC

py5_docs/scripts/create_missing_documentation_files.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@
103103
print(f"creating {new_docfile}")
104104
with open(new_docfile, "w") as f:
105105
extra = (
106-
f"pclass = {pclass}\nprocessing_name = {java_name} # remove if not a part of Processing\n"
106+
f"pclass = {pclass}\nprocessing_name = {java_name} # **REMOVE IF NOT A PART OF PROCESSING**\n"
107107
if java_name
108108
else ""
109109
)

py5_jar/src/main/java/py5/core/Sketch.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,18 @@ public void fakeKeyEvent(int action, int modifiers, String input, boolean isAuto
494494
postEvent(new KeyEvent(null, System.currentTimeMillis(), action, modifiers, key, keyCode, isAutoRepeat));
495495
}
496496

497+
public void interceptEscape() {
498+
if (key == ESC) {
499+
interceptEscape = true;
500+
}
501+
}
502+
503+
@Override
504+
protected void handleKeyEvent(KeyEvent event) {
505+
super.handleKeyEvent(event);
506+
interceptEscape = false;
507+
}
508+
497509
@Override
498510
public void postWindowMoved(int newX, int newY) {
499511
if (!sketchFullScreen()) {

py5_jar/src/main/java/py5/core/SketchBase.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,15 @@ public class SketchBase extends PApplet {
4848
protected boolean disposeCalled;
4949
protected boolean inIPythonSession;
5050
protected boolean inJupyterZMQShell;
51+
protected boolean interceptEscape;
5152

5253
public SketchBase() {
5354
py5Bridge = null;
5455
this.py5RegisteredEvents = new HashSet<String>();
5556
this.py5RegisteredEventParamCounts = new HashMap<String, Integer>();
5657
this.exitActualCallCount = 0;
5758
disposeCalled = false;
59+
interceptEscape = false;
5860
}
5961

6062
public static void setJOGLProperties(String py5Path) {
@@ -97,10 +99,12 @@ public void py5Println(String text, boolean stderr) {
9799

98100
@Override
99101
public void exit() {
100-
if (platform == MACOS && g.isGL() && !isLooping()) {
101-
loop();
102+
if (!interceptEscape) {
103+
if (platform == MACOS && g.isGL() && !isLooping()) {
104+
loop();
105+
}
106+
super.exit();
102107
}
103-
super.exit();
104108
}
105109

106110
@Override

py5_resources/data/sketch.csv

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,7 @@ println,println,,method,output,text_area,PYTHON,override to provide special prin
231231
set_println_stream,,,method,output,text_area,PYTHON,special print tools for managing print output from a running sketch
232232
main,main,,method,,,SKIP,public methods that shouldn't be available to users
233233
exit_sketch,exit,,method,structure,,JAVA,renamed because of conflict with builtin python function
234+
intercept_escape,interceptEscape,,method,structure,,JAVA,
234235
get_pixels,get,@_return_py5image,method,image,pixels,JAVA,
235236
method,method,,method,,,SKIP,
236237
append,append,,method,data,array_functions,SKIP,user should use python instead

0 commit comments

Comments
 (0)