• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • 🏆 Hive's 6th HD Modeling Contest: Mechanical is now open! Design and model a mechanical creature, mechanized animal, a futuristic robotic being, or anything else your imagination can tinker with! 📅 Submissions close on June 30, 2024. Don't miss this opportunity to let your creativity shine! Enter now and show us your mechanical masterpiece! 🔗 Click here to enter!

[Trigger] When Key clicked event

Status
Not open for further replies.
Depends on if you are wanting to use GUI, jass, or LUA.

Firstly, you would need the unit set to a variable to call upon at a later point.

  • HeroVarSet
    • Events
      • Map initialization
    • Conditions
    • Actions
      • Set VariableSet HeroVar = Blood Mage 0001 <gen>
Then the Button Event. Unfortunately, in GUI, with the given events, you are limited to left, right, up, and down arrow keys. This is different in jass and LUA.
I am sure there is a way to work around that using customscripts, but I don't have experience with tying GUI and jass/lua together that way.

  • HotkeyPressed
    • Events
      • Player - Player 1 (Red) Presses the Left Arrow key
    • Conditions
    • Actions
      • Selection - Select HeroVar for Player 1 (Red)
I can't really give you an example in jass as I don't know jass well at all. That might give you a starting point.
 
OSKEY_ is the key event example. OSKEY_P would register the P key button press. As I said, jass side I don't understand myself, however the OSKEY format is used within lua as well. Let me see if I can find the thread on it.

Edit:
OSKEY Tutorial by Tasyen
This explains OSKEYs in detail and lists what keys are usable within wc3 code

Edit 2:
There is a post on the page which lays out jass key events like the following;
JASS:
function Trig_MoveStart_Copy_Actions takes nothing returns nothing
    *InsertActionsHere*
endfunction

//===========================================================================
function InitTrig_MoveStart_Copy takes nothing returns nothing
    set gg_trg_MoveStart_Copy = CreateTrigger( )
    call BlzTriggerRegisterPlayerKeyEvent(trigger,Player(0), OSKEY_W, 0, true)
    call TriggerAddAction(gg_trg_MoveStart_Copy, function Trig_MoveStart_Copy_Actions)
endfunction
 
Last edited:
Level 6
Joined
Jul 12, 2021
Messages
95
All I m saying is that if i copy paste the stuff you wrote there it crashes, even after i add an action.
Hello! I m trying to make a trigger which selects a unit when a key from the keyboard is clicked (for ex F4) and idk how to make the event can someone please help?
Here you go mate.

With the next code and triggers a specific unit is selected when someone presses F4. You can copy the code and triggers to try it or just download and open the map attached in this post.

You can assign almost any key. Feel free to ask me if you want to assign a key but don't how to program it. Remember that most of the function keys (F1-F12) already do something when pressed, so be careful when assigning something to them. Normally F4 selects a fourth hero.

JASS:
function KeyPressActions takes nothing returns nothing
    local oskeytype pressedkey = BlzGetTriggerPlayerKey()
    if (pressedkey == OSKEY_F4) then
call QuestMessageBJ(GetPlayersAll(), 1, "F4 pressed")
call ConditionalTriggerExecute(gg_trg_F4Pressed)

    endif
endfunction



function InitializeHotkey takes nothing returns nothing

    local trigger keytrig = CreateTrigger()

    call BlzTriggerRegisterPlayerKeyEvent(keytrig, Player(0), OSKEY_F4, 0, true)

    call TriggerAddAction(keytrig, function KeyPressActions)

endfunction
  • Untitled Trigger 001
    • Events
      • Map initialization
    • Conditions
    • Actions
      • Custom script: call InitializeHotkey()
  • F4Pressed
    • Events
    • Conditions
    • Actions
      • Selection - Select Paladin 0001 <gen> for Player 1 (Red)
 

Attachments

  • F4 pressed.w3m
    16.9 KB · Views: 13
Here you go mate.

With the next code and triggers a specific unit is selected when someone presses F4. You can copy the code and triggers to try it or just download and open the map attached in this post.

You can assign almost any key. Feel free to ask me if you want to assign a key but don't how to program it. Remember that most of the function keys (F1-F12) already do something when pressed, so be careful when assigning something to them. Normally F4 selects a fourth hero.

JASS:
function KeyPressActions takes nothing returns nothing
    local oskeytype pressedkey = BlzGetTriggerPlayerKey()
    if (pressedkey == OSKEY_F4) then
call QuestMessageBJ(GetPlayersAll(), 1, "F4 pressed")
call ConditionalTriggerExecute(gg_trg_F4Pressed)

    endif
endfunction



function InitializeHotkey takes nothing returns nothing

    local trigger keytrig = CreateTrigger()

    call BlzTriggerRegisterPlayerKeyEvent(keytrig, Player(0), OSKEY_F4, 0, true)

    call TriggerAddAction(keytrig, function KeyPressActions)

endfunction
  • Untitled Trigger 001
    • Events
      • Map initialization
    • Conditions
    • Actions
      • Custom script: call InitializeHotkey()
  • F4Pressed
    • Events
    • Conditions
    • Actions
      • Selection - Select Paladin 0001 <gen> for Player 1 (Red)
Thanks for clarifying this, was planning to when I got home tonight. Hard to do from a phone lol
 
Level 19
Joined
Aug 16, 2007
Messages
881
I made a system that allows GUI users to use the new oskey natives in a GUI environment (you'll be able to detect whenever any key is pressed or released).

The "F4 to select a unit" trigger would look like this with this system:
  • SelectUnitWithF4
    • Events
      • Game - GUIKeyEvent becomes Equal to 1.00
    • Conditions
      • _GUIKeyTrigger Equal to GUIKEY_F4
      • _GUIKeyIsDown Equal to False
    • Actions
      • Selection - Select Footman 0001 <gen> for _GUIKeyPlayer
If you think this could be useful I've uploaded it to the Spells section,
 

Dr Super Good

Spell Reviewer
Level 64
Joined
Jan 18, 2005
Messages
27,202
Be aware that such events require network synchronisation before they fire. As such in multiplayer there will be at least 1 round trip time between you and the server before the conditions and actions of the trigger run. This makes it not really suitable for selecting units or simulating a UI related hot key as there will be a significant delay which can interrupt other commands issued between.
 
Status
Not open for further replies.
Top