text input problem
in
Programming Questions
•
9 months ago
Hi I found the following code on the internet that allows the user to input date:
- String typedText = "your text here";
- PFont font;
- void setup() {
- size(400, 400);
- font = createFont("Helvetica", 18);
- }
- void draw() {
- background(255);
- fill(255,0,0);
- textFont(font,18);
- line(0,0,200,200);
- // this adds a blinking cursor after your text, at the expense of redrawing everything every frame
- text(typedText+(frameCount/10 % 2 == 0 ? "_" : ""), 35, 45);
- }
- void keyReleased() {
- if (key != CODED) {
- switch(key) {
- case BACKSPACE:
- typedText = typedText.substring(0,max(0,typedText.length()-1));
- break;
- case TAB:
- typedText += " ";
- break;
- case ENTER:
- case RETURN:
- // comment out the following two lines to disable line-breaks
- typedText += "\n";
- break;
- case ESC:
- case DELETE:
- break;
- default:
- typedText += key;
- }
- }
- }
For this to work properly a background(255); in draw{} is executed every loop to 'refresh' the sketch and draw the updated text. Is there a simple way how this can be done without using background(255) in the draw method?
In other words if I modify the program above so that it generates a random number as show below, the number will be show in my sketch (it is only shown when background(255) is removed).
- String typedText = "your text here";
- PFont font;
- void setup() {
- size(400, 400);
- font = createFont("Helvetica", 18);
- randomNumber();
- }
- void draw() {
- background(255);
- fill(255,0,0);
- textFont(font,18);
- line(0,0,200,200);
- // this adds a blinking cursor after your text, at the expense of redrawing everything every frame
- text(typedText+(frameCount/10 % 2 == 0 ? "_" : ""), 35, 45);
- }
- void keyReleased() {
- if (key != CODED) {
- switch(key) {
- case BACKSPACE:
- typedText = typedText.substring(0,max(0,typedText.length()-1));
- break;
- case TAB:
- typedText += " ";
- break;
- case ENTER:
- case RETURN:
- // comment out the following two lines to disable line-breaks
- typedText += "\n";
- break;
- case ESC:
- case DELETE:
- break;
- default:
- typedText += key;
- }
- }
- }
- void randomNumber()
- {
- textFont(font,50);
- int i=(int)random(0,10);
- text(i,250,250);
- }
Any help on this issue is highly appreciated thanks guys :)
1