First custom event

Just starting out? Need help? Post your questions and find answers here.
coco2
Enthusiast
Enthusiast
Posts: 489
Joined: Mon Nov 25, 2013 5:38 am
Location: Australia

First custom event

Post by coco2 »

Code: Select all

Enumeration CustomEvents #PB_Event_FirstCustomValue
  #EventMainKeyboardCtrlN
  #EventMainKeyboardCtrlO
  #EventMainKeyboardCtrlQ
  #EventMainKeyboardESC
  #EventMainKeyboardF5
  #EventNewKeyboardReturn
  #EventNewSectionKeyboardReturn
EndEnumeration
Then when I try this:

Code: Select all

AddKeyboardShortcut(#WindowMain, #PB_Shortcut_Control | #PB_Shortcut_N, #EventMainKeyboardCtrlN)
I get the error:
The 'Event' value of AddKeyboardShortcut() has to be between 0 and 64000
The value of #PB_Event_FirstCustomValue is 65536. What am I doing wrong?
User avatar
jacdelad
Addict
Addict
Posts: 2114
Joined: Wed Feb 03, 2021 12:46 pm
Location: Riesa

Re: First custom event

Post by jacdelad »

Exactly what the error message says: your value is too high.
The event is for events, you are using it for shortcuts. There are no reserved shortcuts, so just start your enumeration at 0 or 1.
Good morning, that's a nice tnetennba!

PureBasic 6.30/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/2*DX517, 164TB+82TB+28TB+2TB SSD
Raspi 400/500
coco2
Enthusiast
Enthusiast
Posts: 489
Joined: Mon Nov 25, 2013 5:38 am
Location: Australia

Re: First custom event

Post by coco2 »

Are you sure, it's definitely an event according to the syntax:
AddKeyboardShortcut(#Window, Shortcut, Event)
When I set it as starting at 0 I get unusual behaviour. #EventNewKeyboardReturn works but #EventNewSectionKeyboardReturn doesn't. It just closes the window the shortcut is assigned to.
breeze4me
Enthusiast
Enthusiast
Posts: 679
Joined: Thu Mar 09, 2006 9:24 am
Location: S. Kor

Re: First custom event

Post by breeze4me »

The event parameter is actually a menu item ID. That's why the restriction applies.

https://www.purebasic.com/documentation ... rtcut.html
Event : The number which will be returned by the EventMenu() function. This value has a limited range, from 0 to 64000.
The value returned by the EventMenu() function is a menu item ID.

https://www.purebasic.com/documentation ... uitem.html
MenuItemID : A number to identify this menu item in events and commands like SetMenuItemState(). This value should be between 0 and 65535.
coco2
Enthusiast
Enthusiast
Posts: 489
Joined: Mon Nov 25, 2013 5:38 am
Location: Australia

Re: First custom event

Post by coco2 »

When I do this:

Code: Select all

Enumeration Events
  #EventMainKeyboardCtrlN
  #EventMainKeyboardCtrlO
  #EventMainKeyboardCtrlQ
  #EventMainKeyboardESC
  #EventMainKeyboardF5
  #EventNewKeyboardReturn
  #EventNewSectionKeyboardReturn = 10
EndEnumeration
it works, but when I delete the "= 10" it fails.
breeze4me
Enthusiast
Enthusiast
Posts: 679
Joined: Thu Mar 09, 2006 9:24 am
Location: S. Kor

Re: First custom event

Post by breeze4me »

coco2 wrote: Sat Mar 07, 2026 8:18 am When I do this:

Code: Select all

Enumeration Events
  #EventMainKeyboardCtrlN
  #EventMainKeyboardCtrlO
  #EventMainKeyboardCtrlQ
  #EventMainKeyboardESC
  #EventMainKeyboardF5
  #EventNewKeyboardReturn
  #EventNewSectionKeyboardReturn = 10
EndEnumeration
it works, but when I delete the "= 10" it fails.
The constant enumeration named "Events" should be deleted if an enumeration with the same name already exists, as its values would be appended to the existing ones.
To name a constant enumeration, its values must be sequential menu item IDs, so it should be named something like “Enumeration MenuItemID”.

Code: Select all

Enumeration  ;MenuItemID
  #EventMainKeyboardCtrlN
  #EventMainKeyboardCtrlO
  #EventMainKeyboardCtrlQ
  #EventMainKeyboardESC
  #EventMainKeyboardF5
  #EventNewKeyboardReturn
  #EventNewSectionKeyboardReturn = 10
EndEnumeration


#WindowMain = 0
#WindowMain1 = 1

If OpenWindow(#WindowMain, 200, 200, 200, 100, "")
  OpenWindow(#WindowMain1, 200, 200, 200, 100, "", #PB_Window_ScreenCentered)
  
  AddKeyboardShortcut(#WindowMain, #PB_Shortcut_Control | #PB_Shortcut_N, #EventMainKeyboardCtrlN)
  AddKeyboardShortcut(#WindowMain, #PB_Shortcut_Control | #PB_Shortcut_O, #EventMainKeyboardCtrlO)
  AddKeyboardShortcut(#WindowMain, #PB_Shortcut_Control | #PB_Shortcut_Q, #EventMainKeyboardCtrlQ)
  AddKeyboardShortcut(#WindowMain, #PB_Shortcut_Escape, #EventMainKeyboardESC)
  AddKeyboardShortcut(#WindowMain, #PB_Shortcut_F5, #EventMainKeyboardF5)
  AddKeyboardShortcut(#WindowMain, #PB_Shortcut_Return, #EventNewKeyboardReturn)
  
  AddKeyboardShortcut(#WindowMain1, #PB_Shortcut_Return, #EventNewSectionKeyboardReturn) ;The same key must be assigned to a different window.
  
  
  Repeat
    Select WaitWindowEvent()
      Case #PB_Event_Menu
        Select EventMenu()
          Case #EventMainKeyboardCtrlN
            Debug "#EventMainKeyboardCtrlN"
          Case #EventMainKeyboardCtrlO
            Debug "#EventMainKeyboardCtrlO"
          Case #EventMainKeyboardCtrlQ
            Debug "#EventMainKeyboardCtrlQ"
          Case #EventMainKeyboardESC
            Debug "#EventMainKeyboardESC"
          Case #EventMainKeyboardF5
            Debug "#EventMainKeyboardF5"
          Case #EventNewKeyboardReturn
            Debug "#EventNewKeyboardReturn"
          Case #EventNewSectionKeyboardReturn
            Debug "#EventNewSectionKeyboardReturn"
          
        EndSelect
      Case #PB_Event_CloseWindow
        End
    EndSelect
  ForEver
EndIf
coco2
Enthusiast
Enthusiast
Posts: 489
Joined: Mon Nov 25, 2013 5:38 am
Location: Australia

Re: First custom event

Post by coco2 »

I changed it to MenuItems and it seems to work now.

Maybe the Event enumeration was already used or something?
breeze4me
Enthusiast
Enthusiast
Posts: 679
Joined: Thu Mar 09, 2006 9:24 am
Location: S. Kor

Re: First custom event

Post by breeze4me »

coco2 wrote: Sat Mar 07, 2026 8:40 am Maybe the Event enumeration was already used ...?
Probably. Somewhere in the source code, including the .pbi files, there should be a line that says "Enumeration Events ...".

Another possibility is that while there is no "Enumeration Events" line, there may be values that duplicate existing menu item ID values.

For example, if the following constants are defined, the value of #EventNewSectionKeyboardReturn could become identical to #MenuItem6 or #MenuItem_6, potentially causing malfunction.

Code: Select all

#MenuItem6 = 6
#MenuItem7 = 16
#MenuItem8 = 26
; ...

; or

Enumeration MenuItems 6
  #MenuItem_6
  #MenuItem_7
  #MenuItem_8
  ; ...
EndEnumeration

Enumeration Events
  #EventMainKeyboardCtrlN
  #EventMainKeyboardCtrlO
  #EventMainKeyboardCtrlQ
  #EventMainKeyboardESC
  #EventMainKeyboardF5
  #EventNewKeyboardReturn
  #EventNewSectionKeyboardReturn ;= 10
EndEnumeration

Debug #EventNewSectionKeyboardReturn ; = 6
Olli
Addict
Addict
Posts: 1311
Joined: Wed May 27, 2020 12:26 pm

Re: First custom event

Post by Olli »

I suggest you to identify the menu item otherwise.

file menu : 100
edit menu : 200
help menu : 300

file new : 110
file open : 120
file save : 130
file pref : 170
file quit : 190

file pref directories : 171
file pref colors : 172

edit undo : 210
edit redo : 220
edit cut : 230
edit copy : 240
edit paste : 250

help index : 310
help glossary : 320
help about : 390
help about authors : 391
help about licences : 392

etc... You can also use hexadecimal codes ($100, $101, etc...Maximum value = $FA00), and you can also use 4 digits (1000, 2000, 3000, etc...)

Custom example : editable combo box
User avatar
Piero
Addict
Addict
Posts: 1240
Joined: Sat Apr 29, 2023 6:04 pm
Location: Italy

Re: First custom event

Post by Piero »

Custom events (Enumeration CustomEvents #PB_Event_FirstCustomValue) are for when you use PostEvent
coco2
Enthusiast
Enthusiast
Posts: 489
Joined: Mon Nov 25, 2013 5:38 am
Location: Australia

Re: First custom event

Post by coco2 »

Thanks for clearing it up. I'm trying to read the PureBasic help and I couldn't figure out what it meant
User avatar
Piero
Addict
Addict
Posts: 1240
Joined: Sat Apr 29, 2023 6:04 pm
Location: Italy

Re: First custom event

Post by Piero »

coco2 wrote: Mon Mar 09, 2026 8:03 pm I'm trying to read the PureBasic help and I couldn't figure out what it meant
…but never forget the PDF help;)

How to Install the PureBasic Search Bookmarklet
Create a new Bookmark in your browser’s Bookmarks Bar
Right-click it on Bookmarks Bar and edit the details:
Name: Search PB
URL/Location: Copy and paste the code below entirely:

Code: Select all

javascript:(function(){
  var selected = document.getSelection().toString().trim();
  if (selected === "") {
    selected = prompt("No text selected.\nWhat would you like to search for on PureBasic sites?", "");
  }
  if (selected !== null && selected !== "") {
    window.open('https://www.google.com/search?q=site:purebasic.fr OR site:purebasic.com/documentation ' + encodeURIComponent(selected));
  }
})();
How to Use It
Highlight (select) any command or keyword(s) (like PB_Event_FirstCustomValue or thread mk-soft) on any webpage.
Click the Search PB bookmark in your toolbar; a new tab will open with Google results restricted to the PureBasic forums and official documentation.

Note: If you don’t highlight anything, a pop-up box will appear asking you to type your search term(s) manually.
 
Olli
Addict
Addict
Posts: 1311
Joined: Wed May 27, 2020 12:26 pm

Re: First custom event

Post by Olli »

:) off topic piero :P

Explanation of the first problem of this subject :

Understand the difference between a variable and a meta-variable.

A variable is mainly managed by the coder.
A meta-variable is mainly managed by the compiler.

This means that the compilation will ever fail, and your program will never work, if you want to know and display the real value of a meta-variable in a specific line of your source code.

But a meta-variable could be imaginated as a variable.

Example code :

Here below, you have iteration1 which is a variable name.

And you have iteration2 which is a meta-variable name.

Here we go !

Code: Select all

Enumeration iteration2 100 Step 100
   #var1
   #var2
EndEnumeration

For iteration1 = 10 To 40 Step 10
   Debug iteration1
Next

Enumeration iteration2
   #var3
   #var4
EndEnumeration

Debug #var1
Debug #var2
Debug #var3
Debug #var4
Remark : you can observe that, in this source code, I display manually four constants which are defined automacally.

That is the goal of a meta-variable : define automatically any values of constants.
User avatar
Piero
Addict
Addict
Posts: 1240
Joined: Sat Apr 29, 2023 6:04 pm
Location: Italy

Re: First custom event

Post by Piero »

Olli wrote: Tue Mar 10, 2026 8:43 amA meta-variable is mainly managed by the compiler.
Piero back on topic:

This one is a good example of using a "meta-variable" to avoid enumerations conflict, also if they are in multiple files :)
 
Olli
Addict
Addict
Posts: 1311
Joined: Wed May 27, 2020 12:26 pm

Re: First custom event

Post by Olli »

Please let him have the time to learn. I put the link again here about
a simple custom event.
Post Reply