Skip to content

Commit

Permalink
Merge pull request #1 from anic17/patch-1
Browse files Browse the repository at this point in the history
Added comments + checks + small bug fixes regarding special keypresses
  • Loading branch information
timlg07 authored Jul 11, 2022
2 parents 8cfb34a + fc0f213 commit db3c96b
Showing 1 changed file with 28 additions and 12 deletions.
40 changes: 28 additions & 12 deletions getKey.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,34 @@
#include <stdio.h>
#include <conio.h>

int main(int argc, char *argv[]) {
char c;
if (argc > 1) {
c = atoi(argv[1]);
if (!c && argv[1][0] != '0') {
c = argv[1][0];
int main(int argc, char *argv[])
{
/*
Declared as an integer because getch() returns an integer.
And initialization to zero is used to avoid potential issues.
*/
int c = 0;
if (argc > 1)
{
c = atoi(argv[1]);
if(c < 0)
{
c = abs(c); // Make sure we don't input negative values
}
if (!c && argv[1][0] != '0')
{
c = argv[1][0];
}
}
else
{
c = getch();
if (!c || c == 0xE0) // Special codes (0x0 and 0xE0 require 2 keypresses)
{
c += getch() + 255;
}
}
} else {
c = getch();
}

putchar(c);
return c;
putchar(c % 256); // Make sure to keep the character range from 0 to 255
return c;
}

0 comments on commit db3c96b

Please sign in to comment.