r/C_Programming • u/AskComprehensive7867 • May 21 '26
Checking inputs efficiently in C
im trying to check inputs in a command line program, using windows api here (runs on command prompt or windows powershell)
if (GetAsyncKeyState(VK_UP))
playerY -= moveSpeed;
else if (GetAsyncKeyState(VK_DOWN))
playerY += moveSpeed;
else if (GetAsyncKeyState(VK_LEFT))
playerX -= moveSpeed;
else if (GetAsyncKeyState(VK_RIGHT))
playerX += moveSpeed;
but is there any way to make it more efficient, 4 system level calls for checking something this trivial is very inefficient? i tried using getkeyboardstate(), but it doesnt work in c. Tried the event driven approach, but it says that it goes through more layers and data structures which brings it back to square one even with low syscalls, im sort of confused
need help pls
View all comments
Show parent comment
1
u/AskComprehensive7867 May 21 '26
its the same as mine, 4 syscalls, my main query was if i can somehow reduce it to 1 syscall, then use the output of that to quickly figure out which key was pressed.. Getkeyboardstate() fails, and event driven versions are more slower it says, as in they go through a more number of OS layers than Getasynckeystate() does