cancel
Showing results for 
Search instead for 
Did you mean: 
Data Engineering
Join discussions on data engineering best practices, architectures, and optimization strategies within the Databricks Community. Exchange insights and solutions with fellow data engineers.
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_Fatma
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.

Join 100K+ Data Experts: Register Now & Grow with Us!

Excited to expand your horizons with us? Click here to Register and begin your journey to success!

Already a member? Login and join your local regional user group! If there isn’t one near you, fill out this form and we’ll create one for you to join!