How to simulate a button push by highlighting text?

136 views Asked by At

I am working on a small console calculator project in c using Xcode on Mavericks. I've figured out how to detect a key stroke, now I need a way to highlight text, like changing its background color or something, so when a user pushes a key, 1 for example that key on the calculator is highlighted as long as he holds the key, to create a nice button push simulation, but I don't know how to this, any ideas?:

#include <stdio.h>
#include <unistd.h>
#include <termios.h>


char myGetch() { // an alternative getch function
    char buf = 0;
    struct termios old = {0};
    if (tcgetattr(0, &old) < 0)
        perror("tcsetattr()");
    old.c_lflag &= ~ICANON;
    old.c_lflag &= ~ECHO;
    old.c_cc[VMIN] = 1;
    old.c_cc[VTIME] = 0;
    if (tcsetattr(0, TCSANOW, &old) < 0)
        perror("tcsetattr ICANON");
    if (read(0, &buf, 1) < 0)
        perror ("read()");
    old.c_lflag |= ICANON;
    old.c_lflag |= ECHO;
    if (tcsetattr(0, TCSADRAIN, &old) < 0)
        perror ("tcsetattr ~ICANON");
    return (buf);
}

int main(int argc, const char * argv[]) {
    // printing a simple box
    printf(" ⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼\n");
    printf("⎹⎹    press (q) to quit.    ⎸⎸\n");
    printf("⎹⎹⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎸⎸\n");
    printf("⎹⎹⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎼⎸⎸\n");
    printf("⎹⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎸\n");
    printf("⎹  C  ⎸ ⎸ ±  ⎸ ⎸ ÷  ⎸ ⎸  ✕   ⎸\n");
    printf("⎹⎼⎼⎼⎼⎼⎸⎹⎼⎼⎼⎼⎼⎸⎹⎼⎼⎼⎼⎼⎸⎹⎼⎼⎼⎼⎼⎼⎼⎸\n");
    printf("⎹  7  ⎸⎹  8  ⎸⎹  9  ⎸⎹   -   ⎸\n");
    printf("⎹⎼⎼⎼⎼⎼⎸⎹⎼⎼⎼⎼⎼⎸⎹⎼⎼⎼⎼⎼⎸⎹⎼⎼⎼⎼⎼⎼⎼⎸\n");
    printf("⎹  4  ⎸⎹  5  ⎸⎹  6  ⎸⎹   +   ⎸\n");
    printf("⎹⎼⎼⎼⎼⎼⎸⎹⎼⎼⎼⎼⎼⎸⎹⎼⎼⎼⎼⎼⎸⎹⎼⎼⎼⎼⎼⎼⎼⎸\n");
    printf("⎹  1  ⎸⎹  2  ⎸⎹  3  ⎸⎹       ⎸\n");
    printf("⎹     ⎸⎹     ⎸⎹     ⎸⎹       ⎸\n");
    printf("⎹⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺ ⎸⎹   =   ⎸\n");
    printf("⎹      0    ⎹ ⎹  .  ⎸⎹       ⎸\n");
    printf("⎹           ⎹ ⎹     ⎸⎹       ⎸\n");
    printf(" ⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺\n");
    char c;
    c = myGetch() ;
    printf("%c\n",c);
    return 0;
}
0

There are 0 answers