End keys in c++

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 ?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include<iostream>
#include<cstring>
#include<conio.h>
using namespace std;
int main(){
int a;
system("color a9");


cout<<"PASSWORD= ";
while(a!=13)
{
	a=getch();
	if(a>='a'&&a<='z'||a>='0'&&a<='9'||a>='A'&&a<='Z'||a>=33&&a<=126)
	{	
		cout<<".";
	}
	
	
}
Last edited on
See documentation for getch()
https://docs.microsoft.com/en-gb/cpp/c-runtime-library/reference/getch-getwch
Quote:
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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <iostream>
#include <cstring>
#include <conio.h>
#include <cstdio>
#include <windows.h>

using namespace 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-getwch
int 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";
        }
    }  
}


Second example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <iostream>
#include <string>
#include <conio.h>

using namespace 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-getwch
int 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;
    
    string password;
    
    while (a!=13)
    {
        a = getkey(keycode);
        
    	if ( a>=33 && a<=126 )
    	{	
    		cout << ".";
    		password += a;
    	}
    	
    	if (a=='\b')
        {
            cout <<"\b \b";
            if (password.size())
                password.pop_back();
        }
    
    }
    
    cout <<"\nPswd =    " << password << '\n';
}
Last edited on
Topic archived. No new replies allowed.