@@ -102,6 +102,12 @@ function setup(c) {}
102102function draw (c ) {}
103103```
104104
105+ You can add extra methods ` onKey ` and ` onMouse ` to catch keyboard and mouse events:
106+ ``` js
107+ function onKey (c , e ) {}
108+ function onMouse (c , e ) {}
109+ ````
110+
105111And you can then execute the file with:
106112` ` ` sh
107113./runal -f sketch.js
@@ -133,8 +139,17 @@ func main() {
133139func setup(c *runal.Canvas) {}
134140
135141func draw(c *runal.Canvas) {}
142+ ` ` `
143+
144+ You can add extra methods ` onKey` and ` onMouse` to catch keyboard and mouse events:
145+ ` ` ` go
146+ func onKey(c *runal.Canvas, e runal.KeyEvent) {}
147+ func onMouse(c *runal.Canvas, e runal.MouseEvent) {}
148+ ` ` ` `
136149
137- func onKey (c *runal .Canvas , key string ) {}
150+ And you can then execute the file with:
151+ ` ` ` sh
152+ go run sketch .go
138153```
139154
140155## Examples
@@ -222,6 +237,12 @@ Number of frames rendered since the beginning.
222237#### c.isLooping
223238Boolean indicating whether the render loop is active.
224239
240+ #### c.mouseX <sub >since v0.5.0</sub >
241+ Returns the horizontal mouse position relative to the terminal window.
242+
243+ #### c.mouseY <sub >since v0.5.0</sub >
244+ Returns the vertical mouse position relative to the terminal window.
245+
225246<hr class =" separator " />
226247
227248### Canvas
@@ -474,13 +495,9 @@ The **console.log** file is deleted upon exit.
474495
475496### Events
476497
477- #### onKey(c, event)
498+ #### onKey(c, event) < sub >since v0.5.0</ sub >
478499Listen to keyboard events.
479500
480- Event is an object with 2 attributes:
481- - ** key** : the string representation of the key pressed (` a ` , ` b ` , ` c ` , ` 1 ` , ` 2 ` , ` 3 ` etc...)
482- - ** code** : the numerical value of the key pressed (97, 98, 99, 49, 50, 51 etc...)
483-
484501It's a root function, which means it should be placed at the same level as ** setup()** and ** draw()** .
485502``` js
486503// mySketch.js
@@ -494,7 +511,44 @@ onKey(c, event) {
494511 if (event .key == " space" ) {
495512 saveCanvasToPNG (" canvas.png" );
496513 }
514+
515+ // same using the key code.
516+ if (event .code == 32 ) {
517+ saveCanvasToPNG (" canvas.png" );
518+ }
519+ }
520+ ```
521+
522+ Event is an object with the following attributes:
523+ - ** key** : the string representation of the key pressed (` a ` , ` b ` , ` c ` , ` 1 ` , ` 2 ` , ` 3 ` etc...)
524+ - ** code** : the numerical value of the key pressed (97, 98, 99, 49, 50, 51 etc...)
525+
526+ Key string representations for keys other than alphanumerical works as well. You can use the following strings to handle all your keyboard keys:
527+ ` enter ` , ` tab ` , ` backspace ` , ` esc ` , ` space ` , ` up ` , ` down ` , ` left ` , ` right ` , ` begin ` , ` find ` , ` insert ` , ` delete ` , ` select ` , ` pgup ` , ` pgdown ` , ` home ` , ` end ` , ` kpenter ` , ` kpequal ` , ` kpmul ` , ` kpplus ` , ` kpcomma ` , ` kpminus ` , ` kpperiod ` , ` kpdiv ` , ` kp0 ` , ` kp1 ` , ` kp2 ` , ` kp3 ` , ` kp4 ` , ` kp5 ` , ` kp6 ` , ` kp7 ` , ` kp8 ` , ` kp9 ` .
528+
529+ You can also use ` ctrl+a ` , ` alt+b ` etc...
530+
531+ #### onMouse(c, event) <sub >since v0.5.0</sub >
532+ Listen to mouse click events.
533+
534+ It's a root function, which means it should be placed at the same level as ** setup()** and ** draw()** .
535+ ``` js
536+ // mySketch.js
537+ setup (c ) { ... }
538+
539+ draw (c ) { ... }
540+
541+ onMouse (c , event ) {
542+ // draw a circle at mouse position when
543+ // the mouse left button is clicked.
544+ if (event .button == " left" ) {
545+ c .circle (event .x , event .y , 5 );
546+ }
497547}
498548```
549+ Event is an object with the following attributes:
550+ - ** x** : the horizontal mouse position relative to the terminal window
551+ - ** y** : the vertical mouse position relative to the terminal window
552+ - ** button** : the mouse button that has been clicked. Possible values: ** left** , ** middle** , ** right**
499553
500554</section >
0 commit comments