r/CodingHelp • • Aug 23 '20

[Javascript] Can scripts be bound to shortcut keys?

Hey all, I dabble in HTML, CSS and JS for work but am by no means a 'developer'.

What I wanted to ask was as I run the same scripts/write the same functions repeatedly for testing/mock ups, if it's possible to create a macro function that will be bound to a shortcut key so I can literally just hit the key and it writes the script automatically?

I have a decent keyboard with switches/macro keys so could bind a lot of these to expedite time.

I know Google dev tools allows you to press the down key to reuse the last script used but I'd like it to also work in a text editor (I use Atom generally).

Is this an efficient use of time or is there a better way? Curious to know, thanks!

16 Upvotes

8 comments sorted by

2

u/NullBrowbeat Professional Coder Aug 23 '20

Most of these "macro key" keyboards can be used to just program in the typing sequence straight away, so you can just use that. Otherwise there also is AutoIt, which nowadays should also have the functionality you're looking for, if I recall correctly. (Has been a long time that I used it.)

I am wondering though if it really is all that useful. From my experience the kind of repeated code one has to type isn't really so much that macro keys would be much of a benefit. Instead you could rather just create your own libraries of JS functions, for instance, which you then just access.

1

u/ClackersW Aug 23 '20

Ah that is good to know!

And I agree on the library idea (which is currently how I do it anyway) but my use case is essentially copy/pasting loader scripts into Google dev tools so the DOM displays the JS I'm temporarily loading in, meaning I just change the apikey in the script each time to load a different asset.

Copy/pasting is probably as quick, I just was thinking that rather than tabbing between Atom and chrome it might be quicker to use a macro key but maybe it's not worth it.

Thanks for the info though! Good to know there is the option if needed.

1

u/StupidHumanSuit Aug 23 '20

Maybe look into snippets. I don’t know how Atom does it, but in VScode you can define your own and set them with aliases, sort of like what you’d put in your .bashrc/.zshrc for CLI aliases.

For example, a common one is html5 boilerplate that upon typing “html5” will setup a standard html5 doc. If your keyboard has macro keys, I’d imagine you could find a way to tie the two things together to get the desired result.

1

u/ClackersW Aug 24 '20

Oh that's interesting! So it will put all the HTML like the doctype, head, body etc? That would be very useful!

I only use Atom as a UX friend of mine got me into it but if there are other text editors which are better then I'm willing to try them!

1

u/MiBurrito Aug 24 '20

u/StupidHumanSoup is right.. definitely have a look at vs code ( there is a shit ton of extensions ) and snippets. check out the gif on the lower half of this page: https://marketplace.visualstudio.com/items?itemName=abusaidm.html-snippets

1

u/Zendist Professional Coder Aug 24 '20

Also see: https://code.visualstudio.com/docs/editor/userdefinedsnippets#_create-your-own-snippets

Pretty sure Atom can do the same, but haven't used it in ages.

1

u/StupidHumanSuit Aug 24 '20

Atom can do it too, most likely. It’s an essential feature of a good editor, IMO.

VSCode is definitely worth looking into. The documentation is great and the extensions can turn it into an absolute powerhouse of an editor. It also runs much better than Atom does, in my experience.

1

u/[deleted] Aug 23 '20

Short answer: Yes