System Wide Hotkey - Insert Date & Time Text (Duplicate AHK Script)

15 minutes searching. :eyes:
I cannot find exactly this.
So - New Topic if that's OK

Want to replace as many other programs as possible by bringing all my daily tasks/tweaks into DirOpus.

So, I need a System Wide Hotkey to
insert the date & time (or optionally just date)
into any text field anywhere in Windows

This format :
"2020.09.04 @ 16.42 - "

--

Want to replace this AHK (Auto Hot Key) script:

"+!z::
*FormatTime, CurrentDateTime,, yyyy.MM.dd @ HH.mm - *
SendInput %CurrentDateTime%
return"

--

Thanks for any help possible on this one,
S

See here for a solution: Use Opus to hotkey typing out a key sequence

Thanks Leo,

Followed that link & read it all
Followed a link from inside that link & read all that too :face_with_monocle:

I see how to create a file with the date inserted inside it

How to just have "2020.09.04 @ 17.14 - "
appear wherever my text cursor is
, anywhere in Windows?

  • No new files, just the text :pen:

Sorry, if I'm missing the solution in that link
(& link inside that link).

I just didn't see it, if it's there. :eyes:

Thanks,
S

The exact message Leo linked to explains how to do it.

Oh no.
Really?
I'm sorry. :flushed:

I could see the code to insert it INSIDE a file but I THOUGHT I didn't see how to make it just appear as text

I'll read it all again.

My apologies.

So if I create a hot key with this code:
function OnClick(clickData)
{
var wsh = new ActiveXObject("WScript.Shell");
wsh.SendKeys("{HOME}+{END}");
}
\

Then what - I insert the date/time code from the other thread into a scripot file called WScript.Shell?

Is that right?

--
I'm honestly not dumb :flushed: just more arts than sciences inclined.

I'm trying to figure stuff out\ myself (perhaps with limited success)

Honestly, I read up on, & solved, perhaps 10 distinct issues without ever posting myself. I have like 120 tabs saved trying to find stuff out BEFORE I ever asked for help

My apologies
S

Leo's post shows how you can send keys to other programs from Opus. You'd need to change it to send the date or whatever it is you want to send. Or just stick with the solution you already have.

Keys to other programs - right....

But at the moment it is AHK that turns the key combination (Shift-Z-Alt) into a command to insert date & time in my preferred format wherever I'm typing

I understand how to make system wide hotkeys in DirOpus (I hope)

Unless I have the wrong end of the stick, I don't want to send keys combinations to another program

I want a DirOpus run key combination produce the text "2020.09.04 @ 17.44 - "

That's not the same thing as sending keys, right?
Or is it? :flushed:

S

I don't really know how to explain this any differently. Just stick with the AHK solution you have already.

I just wanted to try to clarify this issue since it still seems to be causing confusion.

You seem to be under the impression that "inserting text at the cursor anywhere in the system" is a built-in function of Opus.

It's not.

Opus does not have a function like that. That's a function of something like AHK.

The one way you can do it through Opus, which is what the thread Leo pointed you to described, is to use a bit of scripting to use the WScript object's SendKeys method to generate key presses.

(The WScript object is also not part of Opus. It's provided by Windows.)

When a text field is active, generating key presses (which makes it appear that you are typing on the keyboard) has the effect of inserting text at the cursor position.

So if you really want to do this through Opus, instead of AHK, you need to take the scripting example that Leo pointed you to, and adapt it to send the key presses corresponding to the text that you want to insert. You then need to set it up on a system-wide Opus hotkey so that pressing that hotkey when Opus is running triggers the script.

But again, and I can't stress this enough, this is not a function that Opus has built-in, it's not something Opus was designed to do, and if you expect it to be possible through a "one word command" you're going to be disappointed.

1 Like

So I read you right it IS possible to replace AHK but not at all easy. Yes?

Once I have made 1 script to insert text would it then become a lot easier to just change the strings inserted and make all the others?

Or will it continue to be a total bear I'm not yet up to managing?

The actual code that sends the virtual key presses is one line (wsh.SendKeys("....")) so changing what it sends is just a matter of editing that line. If you want to send a fixed string it's trivial. If you want to send date & time as you initially asked for you will also need script code to generate that string (not difficult, but another step).

You've got to stop thinking of this as doing it with Opus though. You'd be doing it with scripting and the WScript shell object. Opus is just acting as the launcher here.

Right. Sorry about the technological inexactitude.

To ME, it does FEEL like I am doing it all through DirOpus & trying to pull everything under one roof. Want to purge my system of a hodge-podge of freeware & shareware partial solutions to all my workflow issues. (To the greatest extent possible anyway.)

I've never done any scripting before
so, for me, it IS a novel thing I'm doing in DirOpus.
I'll strive to bear in mind that it isn't, in fact, that.

What is WScript shell, then?

  • part of Windows or the DOS underneath?

Or, have I got totally the wrong end of the stick here?

I have a collection of about 20 key-press shortcuts for text - maybe half half changing like date, time strings
& also have just phrases like
Shift +P+L becomes
"Private & Confidential Legal Advice. If received in error, do not read"

I answered this already:

It's documented here:
https://social.technet.microsoft.com/wiki/contents/articles/5169.vbscript-sendkeys-method.aspx

Hi! Here is the code that will do exactly this:

function zfill2(n) // This function puts "0" prefix to any single-digit number.
{
	return n <= 9 ? "0" + n : n;
}

function OnClick(clickData)
{
	// Build our "date @ time" string
	var today = new Date();	
	var strDate = today.getFullYear() + "." + zfill2(today.getMonth() + 1) + "." + zfill2(today.getDate());
	var strTime = zfill2(today.getHours()) + "." + zfill2(today.getMinutes());
	
	var outString = strDate + " @ " + strTime + " - ";
	//DOpus.Output(outString);

	// Send the "outString" to any currently active window
	var wsh = new ActiveXObject("WScript.Shell");
	wsh.SendKeys("^+%"); // Cancel any held modifiers to prevent garbled text.
	wsh.SendKeys(outString);
}

Just to be sure, even though you already know how to do it, here are step-by-step instructions on how to bind this to a global Shift+P hotkey in Opus:

(click on each Step to expand its image)


Step 1 (click me)

step1

Open the Keyboard Map.


Step 2

Create a new System-wide Hotkey.


Step 3

step3

We'll use Shift+P in this example.
Just press the Shift+P key combination in the Hotkey box, we don't type it in!

Side note: @Leo can we create hotkeys with multiple modifiers, such as Ctrl+Shift+P? I didn't find a way for it. Additionally, compound key combos such as CTRL + K, V are possible for the Toolbars, but not hotkeys?


Step 4

step4

Now you need to select a Function type. There are several possible, select the "Script Function" so that we can use the most advanced way of scripting available in the Opus.


Step 5

step5

  • Under the "Script Type" make sure that the jscript is selected.
    We'll be using the jscript for this example since it offers the combination of power and modern syntax as opposed to the other available option "vbscript" which is also powerful but its syntax is archaic and I forgot it.

  • Delete all the template code that is already there, and replace it with the code I've given you above.

  • Close all the windows (OK buttons) so that our setup is saved, and try pressing the Shift+P in the Notepad, Word or anywhere there is a textbox. Sometimes it may happen that you get garbled text, that's the SendKeys issue (related to the modifier keys being held by you while text is being "sent", I've tried to mitigate it in the code, but... you know how these things go), in that case try pressing it again.

  • You can close all Opus windows and the global hotkey (and our script) will continue to work normally as long as there is an Opus icon in the taskbar tray (i.e., Opus process is active in the background). So Opus can replace the AHK for you.

1 Like

Multiple modifiers: Yes, just type what you want as if you were activating the hotkey. If it doesn't work, something else might already be intercepting that key.

Compound keys: Should work as both toolbar and standalone hotkeys (at least as far as I know), but I doubt they'd work for system-wide hotkeys as that would require taking input focus in order to see the second keypress.

1 Like