473,671 Members | 2,214 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Need help with a javascript virtual keyboard for TextBox

1 New Member
hi i recently used a virtual keyboard from www.codeproject.com/jscript/jvk.asp.
However this part of the code

Expand|Select|Wrap|Line Numbers
  1. function keyb_callback(ch)
  2.    {
  3.      var text = document.getElementById("textfield"), val = text.value;
  4.  
  5.      switch(ch)
  6.      {
  7.        case "BackSpace":
  8.          if(val.length)
  9.          {
  10.            var span = null;
  11.  
  12.            if(document.selection)
  13.              span = document.selection.createRange().duplicate();
  14.  
  15.            if(span && span.text.length > 0)
  16.            {
  17.              span.text = "";
  18.              getCaretPositions(text);
  19.            }
  20.            else
  21.              deleteAtCaret(text);
  22.          }
  23.  
  24.          break;
  25.  
  26.        default:
  27.          insertAtCaret(text, (ch == "Enter" ? (window.opera ? '\r\n' : '\n') : ch));
  28.      }
  29.    }
  30.  
  31.    // This function retrieves the position (in chars, relative to
  32.    // the start of the text) of the edit cursor (caret), or, if
  33.    // text is selected in the TEXTAREA, the start and end positions
  34.    // of the selection.
  35.    //
  36.    function getCaretPositions(ctrl)
  37.    {
  38.      var CaretPosS = -1, CaretPosE = 0;
  39.      ctrl.focus();
  40.  
  41.      // Mozilla way:
  42.      if(ctrl.selectionStart || (ctrl.selectionStart == '0'))
  43.      {
  44.        CaretPosS = ctrl.selectionStart;
  45.        CaretPosE = ctrl.selectionEnd;
  46.  
  47.        insertionS = CaretPosS == -1 ? CaretPosE : CaretPosS;
  48.        insertionE = CaretPosE;
  49.      }
  50.      // IE way:
  51.      else if(document.selection && ctrl.createTextRange)
  52.      {
  53.        // The current selection:
  54.        var range;
  55.        if(ctrl.tagName.toLowerCase() == "input")
  56.        {
  57.          range = ctrl.createTextRange();
  58.        }
  59.        else if(ctrl.tagName.toLowerCase() == "textarea")
  60.        {
  61.          range = document.selection.createRange();
  62.        }
  63.  
  64.        // We'll use this as a 'dummy':
  65.        var stored_range = range.duplicate();
  66.  
  67.        // Select all text:
  68.        stored_range.moveToElementText(ctrl);
  69.  
  70.        // Now move 'dummy' end point to end point of original range:
  71.        stored_range.setEndPoint('EndToEnd', range);
  72.  
  73.        // Now we can calculate start and end points:
  74.        insertionS = stored_range.text.length - range.text.length;
  75.        insertionE = insertionS + range.text.length;
  76.      }
  77.    }
  78.  
  79.    function setRange(ctrl, start, end)
  80.    {
  81.      if(ctrl.setSelectionRange) // Standard way (Mozilla, Opera, ...)
  82.      {
  83.        ctrl.setSelectionRange(start, end);
  84.      }
  85.      else // MS IE
  86.      {
  87.        ctrl.focus();
  88.  
  89.        var range;
  90.  
  91.        try
  92.        {
  93.          range = ctrl.createTextRange();
  94.        }
  95.        catch(e)
  96.        {
  97.          try
  98.          {
  99.            range = document.body.createTextRange();
  100.            range.moveToElementText(ctrl);
  101.          }
  102.          catch(e)
  103.          {
  104.            range = null;
  105.          }
  106.        }
  107.  
  108.        if(!range) return;
  109.  
  110.        range.collapse(true);
  111.        range.moveStart("character", start);
  112.        range.moveEnd("character", end - start);
  113.        range.select();
  114.      }
  115.  
  116.      insertionS = start;
  117.      insertionE = end;
  118.    }
  119.  
  120.    function deleteSelection(ctrl)
  121.    {
  122.      if(insertionS == insertionE) return;
  123.  
  124.      var tmp = (document.selection && !window.opera) ? ctrl.value.replace(/\r/g,"") : ctrl.value;
  125.      ctrl.value = tmp.substring(0, insertionS) + tmp.substring(insertionE, tmp.length);
  126.  
  127.      setRange(ctrl, insertionS, insertionS);
  128.    }
  129.  
  130.    function deleteAtCaret(ctrl)
  131.    {
  132.      // if(insertionE < insertionS) insertionE = insertionS;
  133.      if(insertionS != insertionE)
  134.      {
  135.        deleteSelection(ctrl);
  136.        return;
  137.      }
  138.  
  139.      if(insertionS == insertionE)
  140.        insertionS = insertionS - 1;
  141.  
  142.      var tmp = (document.selection && !window.opera) ? ctrl.value.replace(/\r/g,"") : ctrl.value;
  143.      ctrl.value = tmp.substring(0, insertionS) + tmp.substring(insertionE, tmp.length);
  144.  
  145.      setRange(ctrl, insertionS, insertionS);
  146.    }
  147.  
  148.    // This function inserts text at the caret position:
  149.    //
  150.    function insertAtCaret(ctrl, val)
  151.    {
  152.      if(insertionS != insertionE) deleteSelection(ctrl);
  153.  
  154.      if(document.createEvent && !window.opera)
  155.      {
  156.        var e = document.createEvent("KeyboardEvent");
  157.  
  158.        if(e.initKeyEvent)
  159.        {
  160.          e.initKeyEvent("keypress",        // in DOMString typeArg,
  161.                         false,             // in boolean canBubbleArg,
  162.                         true,              // in boolean cancelableArg,
  163.                         null,              // in nsIDOMAbstractView viewArg, specifies UIEvent.view. This value may be null;
  164.                         false,             // in boolean ctrlKeyArg,
  165.                         false,             // in boolean altKeyArg,
  166.                         false,             // in boolean shiftKeyArg,
  167.                         false,             // in boolean metaKeyArg,
  168.                         null,              // key code;
  169.                         val.charCodeAt(0));// char code.
  170.  
  171.          ctrl.dispatchEvent(e);
  172.        }
  173.      }
  174.      else
  175.      {
  176.        var tmp = (document.selection && !window.opera) ? ctrl.value.replace(/\r/g,"") : ctrl.value;
  177.        ctrl.value = tmp.substring(0, insertionS) + val + tmp.substring(insertionS, tmp.length);
  178.      }
  179.  
  180.      setRange(ctrl, insertionS + val.length, insertionS + val.length);
  181.    }
work well with textarea on IE but many errors arise when used on a textbox

Some errors include
The words are appended to the front of the orginals word
Insertion/delete in the middle does not work

I am on a tight schedule so i hope u guys can help me solve my problem asap.
Thanks
Jul 11 '07 #1
1 4701
Dmitry Khudorozhkov
2 New Member
Hi,

the script you're talking about was updated:

http://www.codeproject.com/jscript/jvk.asp

Advanced callback function (that you quote) was updated too; it doesn't have any problems with either TEXTAREA and INPUT of type="text".

Best regards.
Jul 13 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

3
2366
by: visual basic dummy | last post by:
I have taken a course at the local college. It is called Advanced Languages and this course will cover C++, Perl and Javascript. I must pass this course. In January/Feb we covered a 1000+page textbook. It has been more than 3 years since I took C language. I have already written my final exam for C++ and just barely passed with a 65%. I have a project that is due on 25 March. I really need someone to sit with me to do this project. I need...
2
3256
by: Steven Malcolm | last post by:
Hi I am trying to build an application at work which will use a virtual keyboard on a touch screen. I have searched the archives and found references but nothing to download. If anyone has any examples of a virtual keyboard that I could use that would be great.
8
5040
by: Jo Segers | last post by:
Hi, How can I restrict the keyboard input in a textBox to 0..9? In the keydown event KeyValue is get only. Where can I alter the keyboard input? Mvg,
1
1191
by: Roshawn Dawson | last post by:
Hi, Could someone tell how to force a mouse event using either jscript or javascript? I simply want to allow users to scroll the page after postbacks using the scroll button on either a mouse or the keyboard without them having to click the page first. Thx, Roshawn
7
10279
by: Benton | last post by:
Hi there, I have a text box which will receive its value from a pop-up date picker. The user should not be able to edit this field with the keyboard or mouse. I am using ASP.NET. If I set the readonly property of the textbox to true, it won't let the user change it and it can receive its value from the pop-op calendar, just what I need. Problem is, after a postback, value is lost. So I think Javascript may come to the rescue here. Is...
6
2023
by: dieselmachine | last post by:
Hey, I've been searching for info on this for days now, but to no avail. I'm starting to think it's impossible, but anyway! I've coded a little virtual keyboard, which has two octaves worth of keys on it, and when the keyboard image is clicked, it uses the coordinates to determine what key was pressed, then some stuff happens that we needn't worry about. I want to make the keys play the appropriate note when clicked, and I can't figure...
4
11640
by: Sirisha | last post by:
I have one textbox for enter telephone numbers. i want that text box can accepts only 0 to 9 digits and one sapcace, -,special characters. I want Javascript script validation for that text box.
6
2220
by: =?Utf-8?B?Sm9obiBBdXN0aW4=?= | last post by:
The HP t5520 Windows CE based thin client comes with a browser called 'Internet Explorer' - iehp.exe. It appears to have Java script (Request.Browser.JavaScript = True). I want to use this browser in conjunction with a 'keyboard wedge' magnetic card reader (and no keyboard) to record the arrival of staff and to display information about the employees jobs for the day. The webform has a textbox that must maintain focus so that when a...
2
1755
by: manusiatidakbiasa | last post by:
Hi, I am new in javascript, I am currently building a simple website, and I like to make a keyboard shortcut so when someone press ctrl-1, it will give keyboard focus to a textbox can anyone help me?
0
8474
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
8392
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8912
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...
0
8819
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8597
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
8669
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6222
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5692
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
4222
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...

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.