Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Get KeyCode

Status
Not open for further replies.

SmallProgram

Programmer
Feb 6, 2002
42
0
0
IN
Hello Friends,
Can I know whether there is any method or can u suggest me any procedure for getting the KeyCode(Example Enter Key on Keyboard Corresponds to 13) as soon as we press the key code in a Text-Box.One thing is that if we can get the method for getiing the key code we can bind the Keycode to the KeyPres Method of the Text Box.
The result is eagerly Awaited.
Thanks in Advance
Mallik.
Programmer,Amatuer
 
Found on theTcler's Wiki at page "Example scripts everybody should have" (
Code:
This little script will echo all keyboard events to stdout: 

    bind . <KeyPress> {
        set dec &quot;&quot;; scan %A %%c dec
        puts &quot;keysym:%K prints:%A ($dec)&quot;
    } ;#-------------- and to turn this off again:
    bind . <KeyPress> {}

ulis
 
Well, typically in situations like this, you don't really want the keycode. Instead, you want the keysym. Tcl's event mechanisms use keysyms, not key codes. For those who might not know about these, here's a description of keysyms from the Tcl documentation for the bind command:

&quot;Keysyms are textual specifications for particular keys on the keyboard; they include all the alphanumeric ASCII characters (e.g. “a” is the keysym for the ASCII character “a”), plus descriptions for non-alphanumeric characters (“comma” is the keysym for the comma character), plus descriptions for all the non-ASCII keys on the keyboard (“Shift_L” is the keysm for the left shift key, and “F1” is the keysym for the F1 function key, if it exists). The complete list of keysyms is not presented here; it is available in other X documentation and may vary from system to system. If necessary, you can use the %K notation described below to print out the keysym name for a particular key. If a keysym detail is given, then the type field may be omitted; it will default to KeyPress. For example, <Control-comma> is equivalent to <Control-KeyPress-comma>.&quot;

Here's a quick little application that will let you interactively determine the keysym for any key on your system:

Code:
# -------------------------------------------
#  FILE: events2.tcl
#  DESCRIPTION: An example of creating
#               widget bindings and using
#               event substitutions.
#               Display X/Y coordinates for
#               <Motion> events, and keysyms
#               and printable characters
#               for <KeyPress> events.
# ==========================================
#  Copyright (c) 2001 Ken Jones
# ==========================================

package require Tk

label .display -width 30 -height 3
pack .display -expand yes -fill both

bind .display <Motion> {
    .display configure -text         &quot;Position = %x, %y&quot;
}
bind .display <KeyPress> {
    .display configure -text         &quot;Keysym = %K\nPrintable = %A&quot;
}

focus .display

Playing around with this, you'll note that the keysym generated by pressing the &quot;Enter&quot; key is actually &quot;Return&quot;, a throwback to the good ol' days when people still talked about carriage returns...

Anyway, this means that to create a binding on a widget that would respond to the Enter key being pressed while that widget has keyboard focus, you'd use a bind command like this:

[tt]bind .t <KeyPress-Return> {
# Whatever you want to do here...
}[/tt] - Ken Jones, President
Avia Training and Consulting
866-TCL-HELP (866-825-4357) US Toll free
415-643-8692 Voice
415-643-8697 Fax
 
Thank U Mr.Ulis and Mr.Ken. The solution gave me the answer for the problem.
Thank U once again.
Mallik. Programmer,Amatuer
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top