Page 1 of 1

e.keyCode

PostPosted: Wed Sep 09, 2020 5:23 am
by John Robin Dove
Hi Clifton,

I can use e.keyCode for keydown, keypress and keyup events on a single object like this:
Code: Select all
 <showCode>
  <function name="myKeyPress" event="keypress" params="e">
  <![CDATA[
    alert(e.keyCode);
  ]]>
  </function>
  </showCode>


but this will not work for a group because I must include useTB="true". Is there a TB equivalent of e.keyCode, please? If not I will just have to write a separate script for each object.

John

Re: e.keyCode

PostPosted: Wed Sep 09, 2020 1:18 pm
by Clifton
You have at least two options to accomplish your task. Both of these involve creating group of two fields. The group is named "theFields" and each field is named "myField1" and "myField2" respectively.
  1. Use the following XML to use keyboard event monitoring on the grouped fields.
  2. Use the following XML to put a uniform function call from events monitored within each field in the group.
Depending on your ultimate goal, one method may be better than the other. For reliability, I would recommend the second approach. For a somewhat simpler approach, go with option 1, but be prepared for a possible anomaly depending on what you are trying to achieve. Events on groups can easily get out of control by sending unexpected events.
 

Re: e.keyCode

PostPosted: Thu Sep 10, 2020 8:23 am
by John Robin Dove
Many thanks for the detailed examples which are very useful. What I want to do is filter the keyboard input. This part of my program allows teachers to grade student's work. According to the grading system more or fewer keys can be used. For example if the grading system is numerical the user must not enter letters etc. It was complicated in VB.NET. I think it's even more complicated in javascript. However this is what I've come up with:
Code: Select all
  <overlay2>
  <function name="myKeyDown" event="keydown" params="evt,undef,target,keyCode,key,shift,ctrl" useTB="true">
  <![CDATA[
  sharedActions.txt = tbfunction_pgTBObjGet(target.txt);
  sharedActions.myName = target.name;
    if (keyCode == 13)
    {
    alert("Function to calculate average grade.");
    }
    else if (keyCode == 8 || keyCode == 46)  //backspace, delete.
    {
      if (keyCode == 46)
      {
        var fct = function()
        {
        tbfunction_pgTBObjSet(sharedActions.myName, "text", "");
        }
      setTimeout(fct, 100);
      }
    }
    else
    {
    var charOK;
      if (sharedActions.VArray[8] == 1 ||  sharedActions.VArray[8] == 3 || sharedActions.VArray[8] == 5)  //Numerical grading systems /20 /10 /30 France, UK etc.
      {
        if (keyCode > 47 && keyCode < 58 || keyCode > 95 && keyCode < 106)
        //keyboard 0 to 9, number pad 0 to 9.
        {
        charOK = true;
        }
        else
        {
        charOK = false;
        }
      }
      else if (sharedActions.VArray[8] == 2) //letter grades A+ to F-  (US system now also used in UK and elsewhere.)
      {
        if (keyCode > 64 && keyCode < 71 || keyCode == 16 || keyCode == 20 || keyCode == 187 || keyCode == 189 || keyCode == 107 || keyCode == 109)
        //keyboard A to F or a to f, Shift, CapsLock, keyboard +, keyboard -, number pad +, number pad -.
        {
        charOK = true;
        }
        else
        {
        charOK = false;
        }
      }
      else if (sharedActions.VArray[8] == 2) //Number grades 1 + to 6   (German system) 1 + = best, 6 = worst.
      {
        if (keyCode > 45 && keyCode < 55 || keyCode > 96 && keyCode < 103  || keyCode == 187 || keyCode == 189 || keyCode == 107 || keyCode == 109)
        //keyboard 1 to 6, number pad 1 to 6, keyboard +, keyboard -, number pad +, number pad -.
        {
        charOK = true;
        }
        else
        {
        charOK = false;
        }
      }
     
      if (charOK == true)
      {
      tbfunction_pgTBObjSet("task", "text", keyCode); //Temporary debug.
      }
      else
      {
      tbfunction_pgTBObjSet("task", "text", "No " + keyCode)  //Temporary debug.
        var fct = function()
        {
          if (sharedActions.txt)
          {
          tbfunction_pgTBObjSet(sharedActions.myName, "text", txt);
          }
          else
          {
          tbfunction_pgTBObjSet(sharedActions.myName, "text", "");
          }
        tbfunction_pgSoundLoader("../../beep.mp3", "mySound", 0, "play");
        }
      setTimeout(fct, 100);
      }
    }
  ]]>
  </function>
  </overlay2>

and so far it seems to work quite well.
John

Re: e.keyCode

PostPosted: Thu Sep 10, 2020 8:41 am
by Clifton
The purpose of the PowerPac function validateField() is perhaps what you need. I use it extensively in my work.
The function filters the input of a field as the user types it based on predefined parameter that you set in the function. With XML, you can change the filter definition at any time to change the behavior of the filter. The JavaScript is pretty much already done for you if you use this function.

Re: e.keyCode

PostPosted: Thu Sep 10, 2020 8:44 am
by John Robin Dove
OK, thanks. I'll check it out.