although my code doesn't accept end keys like "Home,End,Del,INS"...when i input end keys such as "Home,INS" it still happends...Can you help me to eleminate end keys ?
When reading a function key or an arrow key, each function must be called twice; the first call returns 0 or 0xE0, and the second call returns the actual key code.
A quick demo example. You would probably want to replace the messagebox with some other code, or just ignore the special characters completely if you don't need them.
#include <iostream>
#include <cstring>
#include <conio.h>
#include <cstdio>
#include <windows.h>
usingnamespace std;
// If special key is pressed, ch will be zero.
// Result is in keycode instead.
// https://docs.microsoft.com/en-gb/cpp/c-runtime-library/reference/getch-getwchint getkey(int & keycode)
{
int ch = getch();
if (ch == 0 || ch == 0xE0)
{
ch = 0;
keycode = getch();
}
return ch;
}
int main()
{
cout << "PASSWORD= ";
int a = 0;
int keycode = 0;
while (a!=13)
{
a = getkey(keycode);
// If special key is pressed, a will be zero.
// Result is in keycode instead.
if (a == 0)
{
char buf[30];
sprintf(buf, "key code = %d", keycode);
MessageBoxA(GetConsoleWindow(), buf, "Special Key", 0);
}
if ( a>=33 && a<=126 )
{
cout << ".";
}
if (a=='\b')
{
cout <<"\b \b";
}
}
}