OnEvent>KEY_UP

Technical support and scripting issues

Moderators: Dorian (MJT support), JRL

Post Reply
user5274
Junior Coder
Posts: 43
Joined: Tue Aug 04, 2020 9:35 am

OnEvent>KEY_UP

Post by user5274 » Tue Aug 04, 2020 9:42 am

Could someone please provide an example how to use this properly?
Because when I run the macro the event is already constantly firing
Thank you

Code: Select all

OnEvent>KEY_UP,`,0,REQUEST_EXIT

SRT>REQUEST_EXIT
  Let>EXCODE=1
END>REQUEST_EXIT

user5274
Junior Coder
Posts: 43
Joined: Tue Aug 04, 2020 9:35 am

Re: OnEvent>KEY_UP

Post by user5274 » Tue Aug 04, 2020 1:01 pm

Technically its not wrong :D the KEY is UP, but now that means any key_up onevent will fire constantly forever. i just figured the KEY_UP event would fire once on the state change from down to up. 5.0.11 was installed over the top of the 5.0.10 trial

User avatar
Dorian (MJT support)
Automation Wizard
Posts: 1348
Joined: Sun Nov 03, 2002 3:19 am
Contact:

Re: OnEvent>KEY_UP

Post by Dorian (MJT support) » Tue Aug 04, 2020 1:26 pm

If you're looking to turn it off - to quote the help file for OnEvent :
To disable an event handler call OnEvent again with the same parameters but instead of the subroutine name issue an empty string. E.g.:



//enable
OnEvent>KEY_DOWN,VK32,0,KeyPress
...
...
//disable it
OnEvent>KEY_DOWN,VK32,0,
..
..
//enable it again
OnEvent>KEY_DOWN,VK32,0,KeyPress
Yes, we have a Custom Scripting Service. Message me or go here

user5274
Junior Coder
Posts: 43
Joined: Tue Aug 04, 2020 9:35 am

Re: OnEvent>KEY_UP

Post by user5274 » Tue Aug 04, 2020 3:07 pm

Ok great, i seem to understand, but I wasn't actually looking to turn it off in such a "High Level" language but I can appreciate the finite control offered. That being said, if I am watching for a single "keypress" then it would look something like this?

Code: Select all

OnEvent>KEY_DOWN,VK32,0,labelKEYDOWN

SRT>labelKEYDOWN
  //turn OFF keydown event
  OnEvent>KEY_DOWN,VK32,0,
  //turn ON keyup event
  OnEvent>KEY_UP,VK32,0,labelKEYUP
END>labelKEYDOWN

SRT>labelKEYUP
  //turn OFF keyup event
  OnEvent>KEY_UP,VK32,0,
  //go do the tasks, all while main loop is still running
  Gosub>GoDoTheKeyPressTask
  //turn ON keydown event
  OnEvent>KEY_DOWN,VK32,0,labelKEYDOWN
END>labelKEYUP

User avatar
Marcus Tettmar
Site Admin
Posts: 7378
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Re: OnEvent>KEY_UP

Post by Marcus Tettmar » Wed Aug 05, 2020 10:28 am

Yeh ALL keys are up initially! You're most likely only ever going to care about a key being up *after* it's been down. So you'd want to track that and either remember the state or enable/disable the handler. Here's an example which monitors the state of the space key:

Code: Select all

Dialog>Dialog1
object Dialog1: TForm
  Left = 607
  Top = 264
  HelpContext = 5000
  BorderIcons = [biSystemMenu]
  Caption = 'CustomDialog'
  ClientHeight = 126
  ClientWidth = 390
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = True
  ShowHint = True
  OnTaskBar = False
  PixelsPerInch = 96
  TextHeight = 13
  object Label1: TLabel
    Left = 160
    Top = 32
    Width = 56
    Height = 13
    Caption = 'Space is up'
  end
end
EndDialog>Dialog1

Show>Dialog1

OnEvent>KEY_DOWN,VK32,0,SpaceDown


Label>loop
  Wait>0.02
Goto>loop

SRT>SpaceDown
  //reset key down handler
  OnEvent>KEY_DOWN,VK32,,
  
  //set key up handler
  OnEvent>KEY_UP,VK32,,SpaceUp
  
  SetDialogProperty>Dialog1,Label1,Caption,Space is Down

END>SpaceDown

SRT>SpaceUp
  //space is up - reset up handler
  OnEvent>KEY_UP,VK32,,
  
  //set key down handler
  OnEvent>KEY_DOWN,VK32,0,SpaceDown
  
  SetDialogProperty>Dialog1,Label1,Caption,Space is Up

END>SpaceUp
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts