Author Topic: Capturing keypresses for use in FRED  (Read 2855 times)

0 Members and 1 Guest are viewing this topic.

Offline zookeeper

  • *knock knock* Who's there? Poe. Poe who?
  • 210
Capturing keypresses for use in FRED
Maybe this would better belong in the modding or FRED subforum, please move if so.

In FotG we needed to detect specific physical keypresses in a couple of places, and in such a case key-pressed doesn't work because the player might have bound their keys differently. I wrote a very simple script to allow that to be done, so I might as well post it here in case someone finds it useful:

Code: [Select]
#Conditional Hooks

$Application: FS2_Open

$On Game Init: [

    keypressed = function(keyname)
        if keypresses[keyname] then
            return 1
        else
            return 0
        end
    end

    keyreset = function(keyname)
        if keypresses[keyname] then
            keypresses[keyname] = nil
        end
    end

]

$State: GS_STATE_GAME_PLAY

$On Gameplay Start: [

    keypresses = {}

]

$On Key Pressed: [

    local key = hv.Key

    keypresses[key] = true

]

#End


Put that in keypresses-sct.tbm, after which you can use the two functions just like key-pressed and key-reset:

Code: [Select]
$Formula: ( when
   ( >
      ( script-eval-num
         "keypressed('Backspace')"
      )
      0
   )
   ( play-sound-from-table 0 0 0 "78" )
)
+Name: PlaySoundOnBackspace
+Repeat Count: 1
+Interval: 1

$Formula: ( when
   ( has-time-elapsed 5 )
   ( script-eval "keyreset('Backspace')" )
)
+Name: ResetBackspace
+Repeat Count: 1
+Interval: 1

The function returns 0 if the key has not been pressed, and 1 if it has been, hence the use of the > operator and script-eval-num.

 

Offline jr2

  • The Mail Man
  • 212
  • It's prounounced jayartoo 0x6A7232
    • Steam
Re: Capturing keypresses for use in FRED
Is there an appropriate place to put this in the wiki?

 

Offline zookeeper

  • *knock knock* Who's there? Poe. Poe who?
  • 210
Re: Capturing keypresses for use in FRED
Is there an appropriate place to put this in the wiki?

Well, that would be the Scripting Examples category. I don't know how I'm supposed to add a new page though.

 

Offline niffiwan

  • 211
  • Eluder Class
Re: Capturing keypresses for use in FRED
I usually create a new page by creating a link to the "doesn't exist yet" new page on an existing page. That gives you a link which lets you add content to the "doesn't exist yet" page and, well, create it.  Personally I think that's counter-intuitive, I'm more used to wiki's that have a "create page" button somewhere.
Creating a fs2_open.log | Red Alert Bug = Hex Edit | MediaVPs 2014: Bigger HUD gauges | 32bit libs for 64bit Ubuntu
----
Debian Packages (testing/unstable): Freespace2 | wxLauncher
----
m|m: I think I'm suffering from Stockholm syndrome. Bmpman is starting to make sense and it's actually written reasonably well...

 

Offline zookeeper

  • *knock knock* Who's there? Poe. Poe who?
  • 210
Re: Capturing keypresses for use in FRED
I usually create a new page by creating a link to the "doesn't exist yet" new page on an existing page. That gives you a link which lets you add content to the "doesn't exist yet" page and, well, create it.  Personally I think that's counter-intuitive, I'm more used to wiki's that have a "create page" button somewhere.

Well, yeah, I figured I could do that, but looking at how the existing script pages don't have any pages linking to them, I also figured that they were likely created via some other means (as opposed to creating a link to page B on some random page A, adding page B, removing link from page A).

 

Offline niffiwan

  • 211
  • Eluder Class
Re: Capturing keypresses for use in FRED
Well, I've learned something. There's a bunch of different ways to create a page without creating a dummy link first, using the search box seems fairly easy.
Creating a fs2_open.log | Red Alert Bug = Hex Edit | MediaVPs 2014: Bigger HUD gauges | 32bit libs for 64bit Ubuntu
----
Debian Packages (testing/unstable): Freespace2 | wxLauncher
----
m|m: I think I'm suffering from Stockholm syndrome. Bmpman is starting to make sense and it's actually written reasonably well...

 

Offline chief1983

  • Still lacks a custom title
  • 212
  • ⬇️⬆️⬅️⬅️🅰➡️⬇️
    • Minecraft
    • Skype
    • Steam
    • Twitter
    • Fate of the Galaxy
Re: Capturing keypresses for use in FRED
I have created new pages by just typing a page name in the URL bar.  FS2 WebUI Standalone and Release Build Process I think were both created in that manner.
Fate of the Galaxy - Now Hiring!  Apply within | Diaspora | SCP Home | Collada Importer for PCS2
Karajorma's 'How to report bugs' | Mantis
#freespace | #scp-swc | #diaspora | #SCP | #hard-light on EsperNet

"You may not sell or otherwise commercially exploit the source or things you created based on the source." -- Excerpt from FSO license, for reference

Nuclear1:  Jesus Christ zack you're a little too hamyurger for HLP right now...
iamzack:  i dont have hamynerge i just want ptatoc hips D:
redsniper:  Platonic hips?!
iamzack:  lays

 

Offline jr2

  • The Mail Man
  • 212
  • It's prounounced jayartoo 0x6A7232
    • Steam
Re: Capturing keypresses for use in FRED
We need a wiki page on how to make wiki pages.   lol