scan a barcode, play a sound on mac


clock_t start, diff, last, delaytime;

    start = clock();
    last = start;

diff = clock() - start;
    delaytime = diff - last;
    int startint = start * 1;
    int diffint = diff * 1;
    int clockint = clock() * 1;
    int lastint = last * 1;
    int delayint = delaytime * 1;
    //int msec = diff * 1000 / CLOCKS_PER_SEC;
    fprintf(logfile, "%s %d %s %d %s %d %s %d %s %d %s", "start:", startint, "clock:", clockint, "diff:", diffint, "last:", lastint, "delay:", delayint, "\n");

last = diff;


 double clo2;
double clo1;

 clo1 = clock();

clo2 = clock();
    double cdiff = clo2-clo1;

fprintf(logfile, "%s %f %s", "cdiff:", cdiff, "\n");

clo1 = clock();

got a timer working finally. had to really think about how I wanted to use this function:

 int gtod1_sec = t1.tv_sec;
    int gtod1_usec = t1.tv_usec;
    int gtod2_sec = t2.tv_sec;
    int gtod2_usec = t2.tv_usec;

    int diff_sec = gtod2_sec - gtod1_sec;
    int diff_usec = gtod2_usec - gtod1_usec;

    if (diff_usec < 0) {
        diff_sec = diff_sec - 1;
        diff_usec = 1000000 - diff_usec;

    fprintf(logfile, "%s %d%s%d %s %d%s%d %s", "gettimeofday:", gtod2_sec, ".", gtod2_usec, "diff:", diff_sec, ".", diff_usec, "seconds\n");

Screen Shot 2017-04-06 at 5.04.14 PM


so what we want is:

if the key is a number, then start the timer.

if the next key is a number, reset the timer.

if the timer ever reaches 100,000 millionth seconds,  (1/10th of a second) that means it’s not a scan anymore, it’s someone typing, or the scan is done.  log the number.end the timer, and play a music file with that string’s filename.

log the number.

end the timer, and play a music file with that string’s filename.

and also:

[pop open the website (if it’s not the top one), and write the number there as well]


references: – a codebase to scan a barcode in xcode on a mac – a fix for xcode – apple docs key event handeling – keylogger example 1 – keylogger example 2* – key logger search results – keylogger 3… this one works – determine if a character is a number in C – strings in C – what window is in focus? – code to inspect mac osx accessibility apit – applescript to bring an application to the front – switch chrome tabs in applescript – launch another program from c – system(“”) command in c to run a shell script/applescript – a bash script in c – c timer – possibly investigate later – global variable – another timer example – double in string format is %f – clock not working for precise timer. look into gettimeofday() – gettimeofday() in mac? – code example in c++ – another code example of gettimeofday() – mac docs for gettimeofday() – no bool type in c, just use int 0 and 1. – create a thread in c – simple string tasks in c – memset to clear


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s