473,785 Members | 2,266 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

keypress event -> control characters

Dear Javascript Experts,

I'm currently implementing Anyterm, a terminal emulator on a web page.
It consists of an Apache module, some XmlHTTP and a bit of Javascript.
The idea is to give you shell access to your server from (almost)
anywhere; other solutions to the problem tend to require access to
ports other than 80 and Java. The very primitive first attempt is at
http://chezphil.org/anyterm/. A more functional version will be
available soon.

My "problem for the day" is how to send control characters. For
example, if the user types Ctrl-D I need to send byte 4 to the server.
There are two sub-problems: first, some of these actions will be
intercepted by the browser so I need to do anything I can to avoid that
and/or provide an alternative entry mechanism (eg press ALT for CTRL).
Second, I need a piece of reasonably browser-independent code that
takes a keypress event, looks at all of its properties, and returns a
byte.

I'm hoping that someone has already done this and can point me at some
existing code. Otherwise, all suggestions are welcome.

Regards,

--Phil.

Jul 23 '05 #1
4 8989
Hi Phil,

Guess it can be done, but forget about onkeypress.... you would need
onkeydown, since onkeypress only fires for a limited set of chars....
onkeydown lets u catch most of the keys before the browser does... ALT
and CTRL could be detected by reading object event's specific
properties for those keys (altKey, altLeft, ctrlKey, ctrlLeft) in the
onkeydown event handler.. Well, I dont really do cross-browser
scripts... I´m kinda of an IE guy cause thats the enviroment used for
my company, but read some Javascript papers and Netscape's DOM on those
events.. that should not be dificult.

Anyway,
http://msdn.microsoft.com/workshop/a...asp?frame=true

[ ]'s
Diego

Jul 23 '05 #2
Replying to my own question...
I need a piece of reasonably browser-independent code that
takes a keypress event, looks at all of its properties, and returns a
byte. [for key combinations including control sequences]


Well as Diego points out onkeypress doesn't do control keys, but on the
other hand if I use onkeydown I have to map shift+a to capital A etc
myself. So I'm now using the following code that uses both onkeypress
and onkeydown; onkeypress is used in most cases but ignores evens when
ctrl is pressed while onkeydown detects only those. This seems to work
in Firefox1.0 and IE6, though in FF the browser default actions (e.g.
Ctrl-A = select all) still seem to fire; can I stop that? I also need
to worry about keys like PgUp/PgDn, arrows etc. Any improvements
welcomed.

function keypress(ev) {
if (!ev) var ev=window.event ;

if (ev.ctrlKey) {
return;
}

var kc;
if (ev.keyCode) kc=ev.keyCode;
if (ev.which) kc=ev.which;
//if (kc==13) kc=10;
var k=String.fromCh arCode(kc);
process_key(k);

ev.cancelBubble =true;
if (ev.stopPropaga tion) ev.stopPropagat ion();
return false;
}
function keydown(ev) {
if (!ev) var ev=window.event ;

if (!ev.ctrlKey || ev.keyCode==17) {
return;
}

var kc=ev.keyCode;
process_key(Str ing.fromCharCod e(kc-64));

ev.cancelBubble =true;
if (ev.stopPropaga tion) ev.stopPropagat ion();
return false;
}
document.onkeyp ress=keypress;
document.onkeyd own=keydown;

Many thanks,

--Phil.

Jul 23 '05 #3


ph*******@treef ic.com wrote:
though in FF the browser default actions (e.g.
Ctrl-A = select all) still seem to fire; can I stop that?


You could at least try to use the proper code for that
if (ev.preventDefa ult) {
ev.preventDefau lt();
}
in DOM Level 2:
<http://www.w3.org/TR/DOM-Level-2-Events/events.html#Eve nts-Event>
Note that I haven't checked whether that prevents the text selection in
Mozilla.


--

Martin Honnen
http://JavaScript.FAQTs.com/
Jul 23 '05 #4
>> the browser default actions (e.g. Ctrl-A = select all) still seem to
fire
ev.preventDefau lt();


You're right, I should do that. But unfortunately it doesn't seem to
help with ctrl-A.

--Phil.

Jul 23 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
4177
by: Thomas | last post by:
Hey y'all, I'm using firebird 0.7 and developed some kind of inline editing with java script. Therefore I used the keypress event listener. The problem is that I can only enter one character then the "find as you type" starts. You can disable it, of course, but what if someone else uses the web page and hasn't disabled the feature? Do you have an idea how to disable this feature with javascript? How can I get around this? Thx for your...
3
7379
by: Darryn Ross | last post by:
Hi, I am trying to catch the KeyPress event on my datagrid but it isn't working... i have also tried registering the handler with the event like this... dgGLBatch.KeyPress += new KeyPressEventHandler(dgGLBatch_KeyPress); but nothing changed. I was wondering wether a custom table and column style might have an effect on how this works???
15
4057
by: Adam J. Schaff | last post by:
I have noticed that if a user closes a form via pressing return (either while the OK button has focus or if AcceptButton is set to OK for the form) then the "ENTER" keypress event fires ON THE CALLING FORM! This is very bad for me, because in my application, Form1 responds to an ENTER keypress by calling Form2. If the user closes Form2 via an ENTER, then Form1 just reopens it again, kind of trapping the user in Form2 (they can still close...
3
3072
by: Terry Olsen | last post by:
Is there anyway to cause a KeyPress event in a datagrid cell? The only way I get a keypress event is if none of the cells are selected. I was hoping to have the program respond to a certain keypress while in a cell of a datagrid but it doesn't fire the keypress event.
3
1875
by: Just Me | last post by:
I noticed that I don't get KeyDown nor KetPress events if NumLock is not on. I can probably find a workaround but it would be nice to know what the rules are. Can anyone tell me when KeyPress event is raised and when it isn't. Thanks
2
8002
by: Bob | last post by:
In that event, how can you find out if the keypress event was in a certain column? Say you have datagridview1, columnname1, columnName2 You want to do something only with the keypress event of column2, do nothing special with column1 Private Sub Datagrid1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Datagrid1.KeyPress If Me.datagrid1......... Then - how do you specify its for...
5
3083
by: Henry Jones | last post by:
I am new to C# and wanted to capture the KeyPress for a textbox. I created some code as follows: private void textBox3_KeyPress(object sender, System.EventArgs e) { this.textBox2.Text = sender.ToString();
3
2140
by: windy | last post by:
I got a question about keypress event on textbox: I found that the keypress event doesn't invoked when i press the "." button on alphabetic keyboard, however if i use numberpad's "." button keypress event is invoked. Any idea and solutions? Thanks.
1
3108
by: Kid Programmer | last post by:
Hello guys. I was wondering how you make an event happen when the user presses a key. I have seen code used for this but I don't understand how it works. Here is the code in a sample I found: import java.awt.*; import java.awt.event.*; public class KeyPress extends Frame{ Label label; TextField txtField; public static void main(String args) { KeyPress k = new KeyPress();
0
9647
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10356
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10100
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8988
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6744
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5396
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4061
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3665
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2893
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.