Detecting a key combination


 
Thread Tools Search this Thread
Top Forums Programming Detecting a key combination
# 1  
Old 11-30-2006
Detecting a key combination

Could anybody tell me how I can detect a particular key combination and perform a particular task on that event. e.g. if I press Ctrl + L on the shell then it clears the screen. Please tell me how it can be performed on my shell.
And how the Arrow Keys can be detected. I tried but pressing a key once is returning 3 values namely 26,27,(A,B,C,D) The last one differs only.

Please tell me how to call a function or to execute a line of code when a particular event occurs.




Thanks
# 2  
Old 11-30-2006
Have a look at the curses library and it's getkey() function.
# 3  
Old 11-30-2006
There are only so many of these that you can define, those things are defined for you in the man page for stty.

AS an example, suppose you want the backspace key to delete the previous character, then put a line like this in your .profile or .bashrc file in your home directory. The next time you login, it takes effect - or you can source the file to make it work right away

Code:
stty erase "^H"

The erase command tells stty to have your terminal erase a character - "^H" written just the way I have it. You need to define the keys the way you want them.

stty functions are part of the termios set of functions - try termios if you want to do this in C.
# 4  
Old 11-30-2006
Some more help

Could you please clarify my problem
I am making a program in C
Now I am getting the string from the user then How I can put it in that state that what user has hitted.
Any sample will me a great help




Thanks
# 5  
Old 12-17-2006
Please Help

How I can detect a particular Key Combination through programming in Unix.

to trap the signals there are methods but control+L is not detected as a signal so how can I detect that combination of Key Hit and how to detect the Arrow Key Hits as each key hit is itself returning three values
The first two values of all four keys is same only the last key value is different.

I tried to use the getkey function of curses and select and poll funtions but my problem is not solved please help...



Thanks
# 6  
Old 12-17-2006
Let's try this again
1. all keystrokes go thru the tty driver. Period. If your driver interprets them they are interpreted BEFORE you program ever sees them. termios() can remove/add settings.

2. For 2 key ctrl sequences [A-Z] the terminal returns ASCII 1-26 (minus those interpreted by the driver as something else) ctrl-L is 12, ASCII form feed. There are other two-key sequences as well.

3. If you want to be able to create special responses to other key sequences like the function keys or alt key combinations you use the curses or ncurses library.

In general, if you want to play with single key combinations like a GUI does, you have to set the terminal to non-canonical mode. I've posted the code to do that on the forums several times - you can search for it.
# 7  
Old 12-17-2006
Quote:
Originally Posted by jim mcnamara
In general, if you want to play with single key combinations like a GUI does, you have to set the terminal to non-canonical mode. I've posted the code to do that on the forums several times - you can search for it.
Also, I have posted the code to do that, but only one time. But I also wrote a FAQ article, scripts/programs/code posted to www.unix.com, which has a link to it. So if mobile01 had either looked at the faqs or used the search function, he would already have the answer.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Kill -9 -1 combination

Good morning, In a Production environment ive seen this command that kills processes kill -9 -1 Because i am in a production environmet i can not execute this comamnd, so i would like to know what is the difference for the conventional kill -9 PID ? Thanks a lot (11 Replies)
Discussion started by: alexcol
11 Replies

2. Shell Programming and Scripting

Combination of 6 nos

Hi folks, I have a numbers from 1-100 and from these nos I have 30 numbers.. From this 30 nos, I have to generate a combination of 6 nos... this 30 numbers will range from 1-100... ( FYI: This is not a lottery game - just kidding) ... I am trying out this in a shell script.. any ideas ? (3 Replies)
Discussion started by: gsiva
3 Replies

3. Programming

6 digits combination

Is there any program that can create 6 digit numbers with: (DIGIT_1)+(DIGIT_2)+(DIGIT_3)+(DIGIT_4)+(DIGIT_5)+(DIGIT_6)=10 Any perl or C also can. Anyone can help me? Thank you (6 Replies)
Discussion started by: Tzeronone
6 Replies

4. Shell Programming and Scripting

Shell Script @ Find a key word and If the key word matches then replace next 7 lines only

Hi All, I have a XML file which is looks like as below. <<please see the attachment >> <?xml version="1.0" encoding="UTF-8"?> <esites> <esite> <name>XXX.com</name> <storeId>10001</storeId> <module> ... (4 Replies)
Discussion started by: Rajeev_hbk
4 Replies

5. Shell Programming and Scripting

Detecting key combination in bash

I need to grab key combination from a bash script and store it in a variable. Is there any way to do this? Thanks in advance! (1 Reply)
Discussion started by: prism1
1 Replies

6. Solaris

Solaris 8 ssh public key authentication issue - Server refused our key

Hi, I've used the following way to set ssh public key authentication and it is working fine on Solaris 10, RedHat Linux and SuSE Linux servers without any problem. But I got error 'Server refused our key' on Solaris 8 system. Solaris 8 uses SSH2 too. Why? Please help. Thanks. ... (1 Reply)
Discussion started by: aixlover
1 Replies

7. UNIX for Dummies Questions & Answers

Starting terminal with shortcut key combination

How can the shortcut keys be defined that would open up a terminal window? When using a kvm switch, the mouse sometimes does not work, but the keyboard does, and by opening up a terminal window using a shortcut key combination, the mouse can be restarted by entering the predefined mouserestart... (0 Replies)
Discussion started by: figaro
0 Replies

8. UNIX for Dummies Questions & Answers

Pressing backspace key simulates enter key

Hi, Whenever i press the backspace key, a new line appears, i.e. it works like a enter key. :confused: Thanks (2 Replies)
Discussion started by: vibhor_agarwali
2 Replies

9. SuSE

Disabling interrupt function of Control-C key combination

I am using informix RDBMS over SUSE LINUX. In linux if you press control-c it acts as an interrupt key. In my program I have used control-c to perform certain functions but it is being overriden by interrupt function of control-c key combination of SUSE LINUX. Kindly suggest me a solution by which... (1 Reply)
Discussion started by: V.V.KUMAR
1 Replies
Login or Register to Ask a Question