Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
IWButtons Hotkey property and the &-sign in Captions
#1
Hi All,

Would anybody know which values are valid for the HOTKEY property of an IWButton, and also if there are limits to which is available depending on which browser used ?

I want to allow the users to press a hotkey (F10, Alt+A, ESC), in any of the input fields on screen, to active the event behind a corresponding iwbutton, but have so far been unsuccessful.

Also, trying the &-sign in front of a character in an IWButtons Caption, do not select and execute the button event, but rather open or close the menu line on the browser. In my case the Internet Explorer 11. For Edge nothing appears to happen. I have not tried with Firefox or Chrome yet, but expect it to be the same.

Any help is appreciated.

Regards
Soren
Reply
#2
Hi Soren. You can put this code in your form:
Code:
<head>
  <script>
    $(function() {
      document.body.onkeydown = function(e){
        if (e.keyCode == 121) {
          $("#IWBUTTON_GRAVAR").click(); /*F10*/
        }
      };
    });
  </script>
</head>
Reply
#3
(09-20-2019, 10:43 AM)Jose Nilton Pace Wrote: Hi Soren. You can put this code in your form:
Code:
<head>
  <script>
    $(function() {
      document.body.onkeydown = function(e){
        if (e.keyCode == 121) {
          $("#IWBUTTON_GRAVAR").click(); /*F10*/
        }
      };
    });
  </script>
</head>

Hi Jose,

It works nicely. Thanks. Also with (e.key === "F10"), which is a bit easier than remembering all the keycodes.

I have tried with (e.keycode == 27) and (e.key === 'Escape') as well, trying to trap the ESC key to click the "cancel" button, but it do not work. It's like the ESC keydown is being blocked before getting to the function. Do you know if there is anything else one has to do to get the ESC to be passed to the function ?

Regards
Soren
Reply
#4
Wrong:
(e.keycode == 27)

Right:
(e.keyCode == 27)

JS is case sensitive and uses camelCasing. For thoughts on this, check this out:
https://www.kudzuworld.com/articles/gene...nsitivity/
Reply
#5
Hi Kudzu,

Or do you prefer I say Hi Chad (or chad) after reading your article.

The coding was wrong and as soon as I changed the c to a C in keyCode, the script worked as expected. I can see what camelCasing is, but fail to see the benefit of it. However, being use to a case insensitive programming language, I learned to use something similar from start. Using capital letter for every part of a word with different meaning. Like KeyCode, GetCustomer, PostItem, SaveItemsToFile and so on. I do also distinguish spelling between vars, types and procedure/function names, but mostly in the words I use. Using saying names, even though they do become a bit longer than necessary. All in the interest of understanding when reading the code 3-4-5 year later...

Thanks for the simple, yet completely unnoticed difference in the script. I'm sure it would have taken me a long time to find it.

And thanks for the link to the interesting article, to which I fully agree. A lot of time have been, and will continue to be wasted in development.

Regards
Soren
Reply
#6
(09-20-2019, 09:02 PM)SorenJensen Wrote: I can see what camelCasing is, but fail to see the benefit of it.

Sometimes it is best not to spend a lot of energy on benefit of things in JS.  <g>

Dan
Reply
#7
Hi All,

I thought this was all done, sealed and bagged, but no. There is a problem with the Escape key handling, I do not quite get:

I have a form, IWForm1, with some edit fields and 2 buttons. One to save the data, and one to discard editing and release the form.

When pressing F10, the Button BTNGEM.onclick is executed and correctly checks for possible errors in editfields, before saving. If any, saving is discarded with a message and otherwise the data is saved. The form stays open with all data still in the edit fields. This part is working correctly both with F10 and when clicking the button with the mouse.

When pressing ESC, the button BTNAFD.onclick is executed and checks for any changes. If no changes, the form is released. if any changes though, it shows a warning dialog: "you have changed data. do you want to save before leaving ?" with a yes/no option. if user selects no, then changes are is discarded and the form is released. If the users selects yes, the data is saved and the form is released.

Pressing ESC do go through all the tests, but do not show the warning (not even just a simple Webapplication.showmessage I tried instead) whereas it do work correctly when clicking the Button BTNAFB with the mouse. Debugging as far as I can, it do execute the ShowWarning line, but never show the warning. It is like the ESC value / default behaviour, is sent to the warning dialog, which automatically selects no and terminates, even before the warning dialog was rendered(or at least so quick I do not see the dialog appear).

If it is the ESC value being sent on to further processing, is there a way to stop the keyhandling as part of the onkeydown event ?

Or do any of you have an answer to what is causing the behaviour ? As you can see from the JS code, I'm trying to avoid further handling, but it appears not to be successful.

Below is the code I am using:

In the Form.Javascript I have the following lines:

window.addEventListener("keydown", function (event)
{
  if (event.defaultPrevented)
  {
    return; // Do nothing if the event was already processed
  }
  switch (event.keyCode)
  {
    case 121:
      event.returnValue = false;
      event.keyCode = 0;
      $("#BTNGEM").click();
      break;
    case 27:
      event.returnValue = false;
      event.keyCode = 0;
      $("#BTNAFB").click();
      break;
    default:
      return;
  }
  event.preventDefault();
},
true);


and on the form, I have to two buttons with the following OnClick events:

procedure TIWForm1.BtnAfbClick(Sender: TObject);
begin
  if DataChanged then
  begin
    ShowVarning; 
  end else
  begin
    Release;
  end;
end;

procedure TVedlVognF.BtnGemClick(Sender: TObject);
begin
  if IWEdit1.Text = '' then
  begin   
    Webapplication.ShowMessage('Error: You must fill in the name field !');
  end else
  begin
    dm.SaveData;
  end;
end;


Regards
Soren

Hi all again,

I have solved the problems as described above:

By Adding a SLEEP function:

function sleep(delay)
{
  var start = new Date().getTime();
  while (new Date().getTime() < start + delay);
}


and calling it with 100 ms as delay, just before executing the $("#BTNAFB").click() event in the keydown function:

case 27:
  event.returnValue = false;
  event.keyCode = 0;
  sleep(100);
  $("#BTNAFB").click();
  break;


Now, pressing ESC do execute the warning and correctly responds to the users choice. 

A small delay in time, but with a huge impact on program execution, not to mention wasted development time <G>

Regards
Soren
Reply
#8
Hi Soren,

this is an C++ example of handling a button event from an edit control
which also requires little effort and of course can be implemented in Delphi:

void __fastcall TIWFormEdit::IWEditAsyncKeyDown(TObject *Sender, TStringList *EventParams)
{
    if(vkF10 == EventParams->ValueFromIndex[EventParams->IndexOfName(L"which")].ToInt())
    {
        // Call your corresponding button event handler
        IWButtonClick(Sender);
    }
}

Regards
Juergen
Reply
#9
"I can see what camelCasing is, but fail to see the benefit of it."

There is no benefit. Its an illusion and false premise like too many other things in our field currently Sad
Reply
#10
(09-21-2019, 01:36 PM)JuergenS Wrote: Hi Soren,

this is an C++ example of handling a button event from an edit control
which also requires little effort and of course can be implemented in Delphi:

void __fastcall TIWFormEdit::IWEditAsyncKeyDown(TObject *Sender, TStringList *EventParams)
{
    if(vkF10 == EventParams->ValueFromIndex[EventParams->IndexOfName(L"which")].ToInt())
    {
        // Call your corresponding button event handler
        IWButtonClick(Sender);
    }
}

Regards
Juergen

Nice simple solution. Thanks.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)