cancel
Showing results for 
Search instead for 
Did you mean: 
Data Engineering
cancel
Showing results for 
Search instead for 
Did you mean: 

How to distinguish arrow-key from escape character with getch in C?

missyT
New Contributor III

I want to know weather an arrow key or the escape character has ben pressed. But in order to check which arrow key has been pressed I need to do multiple blocking getch-calls bc the arrow-key sequence is bigger than 1 char. This is a problem when I check for the escape char bc it only needs 1 char and this first char is identical to the one arrow returns. So I call the first getch which gets the escape character but afterwards I have to call getch two more times to check its not an arrow-key. Is there a way to know weather escape has been pressed without doing two more getch-calls afterwards to check for the arrow-keys <a href = "https://azar.pro"> Azar </a>?

<a href = "https://chatrandom.download/"> Chatrandom </a>

Thanks

1 ACCEPTED SOLUTION

Accepted Solutions

Hubert-Dudek
Esteemed Contributor III

getch () function returns two keycodes for arrow keys. Arrow put to getch '\033'

and '[' and letter from A to D (up, down, right, left) so code will be something like:

if (getch() == '\033')  { 
    getch(); // [ value
    switch(getch())
    { 
        case 'A':
            // your code
        case 'B':
            // your code
        case 'C':
            //your code
        case 'D':
            //your code
    }
}

View solution in original post

2 REPLIES 2

Hubert-Dudek
Esteemed Contributor III

getch () function returns two keycodes for arrow keys. Arrow put to getch '\033'

and '[' and letter from A to D (up, down, right, left) so code will be something like:

if (getch() == '\033')  { 
    getch(); // [ value
    switch(getch())
    { 
        case 'A':
            // your code
        case 'B':
            // your code
        case 'C':
            //your code
        case 'D':
            //your code
    }
}

Kaniz
Community Manager
Community Manager

Hi @Missy Trussell​ , Did @Hubert Dudek​ 's reply answer your question?

If yes, would you like to mark his reply as the best answer?

Thanks.

Welcome to Databricks Community: Lets learn, network and celebrate together

Join our fast-growing data practitioner and expert community of 80K+ members, ready to discover, help and collaborate together while making meaningful connections. 

Click here to register and join today! 

Engage in exciting technical discussions, join a group with your peers and meet our Featured Members.