Call up a program with a shortcut key lazarus / 21-General ---------------------------------------------------------------------- w click · 2022-06-08 12:49 I want to be able to bring my program to the top, when using another program, with a shortcut key, preferably with a function key.  And access whatever word is under the cursor. e.g. A rather good thesaurus program, Wordweb, does this. I've no idea what this would be called, so my searches have been fruitless.  Any ideas? ---------------------------------------------------------------------- 440bx · 2022-06-08 13:47 It would help if you stated what O/S you're using, that makes a big difference. Since you mentioned Wordweb, it's likely you're using Windows.  In that case, there are a number of ways of getting what you want done. search for "Global hotkey", you'll get plenty of results and different ways of setting up a hotkey for any program you want. HTH. ---------------------------------------------------------------------- w click · 2022-06-08 17:07 Oops, I'm using Windows. I found https://forum.lazarus.freepascal.org/index.php/topic,16436.msg 255366.html#msg255366 and a few others straight away, thanks.  Works well, except only with a modifier, e.g. Alt. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   If Not RegisterHotKey(Handle, 1, MOD_ALT, vk_f10)  Then ShowMessage('HotKey failed...');  Reading around, it appears that '0' will replace 'MOD_ALT', but it's always 'HotKey failed...'.  How do I make it just F10 (or whatever)? ---------------------------------------------------------------------- 440bx · 2022-06-08 17:57 if the call is failing, I suggest that in addition to showing a message, you call GetLastError (before calling ShowMessage, IOW, first thing after the failed RegisterHotKey call) to get an indication from Windows why the call failed.  Show the error or just look it while debugging. HTH. ---------------------------------------------------------------------- w click · 2022-06-08 21:42 Ah ha-ha, thanks.  It works perfectly once I'd taken that hotkey out of autohotkey's script. Anyone any thoughts on picking up what's under the cursor in the program interrupted? ---------------------------------------------------------------------- 440bx · 2022-06-09 03:58 Quote from: w click on June 08, 2022, 11:42:06 pmAnyone any thoughts on picking up what's under the cursor in the program interrupted? for a graphical application, I don't know of an easy/straightforward way.  The reason is, only the application being interrupted has kept track of what word/character is indicated by the cursor (or caret, if it is text).  For an external program, the location of the cursor/caret only indicates a pixel and nothing about what word (if any) that pixel is a part of. In some cases, if the program is using text, sending a WM_GETTEXT might solve the problem for you but, it's far from a general solution.  It will only work in a few cases. HTH. ---------------------------------------------------------------------- marcov · 2022-06-09 10:24 Delphi has a TApplicationEvents object with an onshortcut method that abstracts from the winapi, and also a method that is called on enter/exit a model form. The combination can be used to let the shortcuts only work when not in a modal form. The DFM looks like this: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   object ApplicationEvents1: TApplicationEvents    OnModalBegin = ApplicationEvents1ModalBegin    OnModalEnd = ApplicationEvents1ModalEnd    OnShortCut = ApplicationEvents1ShortCut    Left = 312    Top = 104  end ---------------------------------------------------------------------- w click · 2022-06-09 10:25 WM_GETTEXT or, looking around, WM_COPY or WM_COPYDATA might be it with a SENDMESSAGE.  Unfortunately, this is way out of my comfort zone and looking things up has merely confused me. I only need it to work in Word, so text only. I need a s:=wordundercursor; ---------------------------------------------------------------------- 440bx · 2022-06-09 10:42 Quote from: w click on June 09, 2022, 12:25:32 pmI only need it to work in Word, so text only. I need a s:=wordundercursor; Since you only need it to work with Word, one possibility that comes to mind would be to send Word window messages to select and copy the text under the cursor into the clipboard then retrieve it from the clipboard. Something along the lines of sending Word a double-click for it to select the text followed by a ctrl-c to have it copy it to the clipboard. HTH. ---------------------------------------------------------------------- w click · 2022-06-09 13:45 That would work, and does work if I select the text first.  Thank you. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i s:=widestring(wordapp.Selection.Text);  However, how do I send Word a double click? ---------------------------------------------------------------------- 440bx · 2022-06-09 14:55 Quote from: w click on June 09, 2022, 03:45:59 pmHowever, how do I send Word a double click? You need to know Word's window handle.  Presuming you have that, you can get the current cursor position (which hopefully is over a word) and then use PostMessage to send a double click to the window.  Something along the lines of PostMessage(, WM_LBUTTONDBLCLK, 0, MAKEDWORD(x, y) {<- the cursor position you obtained earlier}). On second thought, it might be better to use SendMessage because the inherent delay in PostMessage could cause a synchronization headache. Try both but, PostMessage is "friendlier" to the app. HTH. ---------------------------------------------------------------------- w click · 2022-06-10 12:55 I assume you meant 'MAKEWORD'. I can't get this to work, the postmessage or sendmessage, I tried both, gives me an exception class 'EVariantError' with message Invalid variant type cast'.  I separated everything out so that I was using variables of the right type. I've also realised that it's not the mouse position that I want, but the text cursor that needs to do the equivalent of a double click to select the word.  The mouse and text cursor are, of course, the same when you do the double-click manually. So, it's windows.caretpos, I think, and some text select operation. ---------------------------------------------------------------------- w click · 2022-06-10 13:28 Bingo!  Except I don't know how to do it – sigh. I need to send to the wordapp three characters or keypresses: Ctrl- right_arrow, ctrl-left_arrow and then ctrl+shift-right_arrow.  (Insane, I know, but that's Microsoft word for you.)  Then s:=widestring(wordapp.Selection.Text);  will work. So, how do I send these actions to Word? ---------------------------------------------------------------------- 440bx · 2022-06-10 14:41 Quote from: w click on June 10, 2022, 02:55:46 pmI assume you meant 'MAKEWORD'. No, I meant DWORD.  The lParam is 32bit (DWORD) in a 32bit program and 64bit (ptrint) in a 64bit program.  in either bitness the (x,y) coordinate is sent in a DWORD which in 64bit is automatically promoted to a ptrint. Quote from: w click on June 10, 2022, 02:55:46 pmI can't get this to work, the postmessage or sendmessage, I tried both, gives me an exception class 'EVariantError' with message Invalid variant type cast'.  I separated everything out so that I was using variables of the right type. there is no reason to use variants for this.  A simple DWORD will do fine (PostMessage doesn't use variants, all parameters are plain ordinals) Quote from: w click on June 10, 2022, 03:28:18 pmI need to send to the wordapp three characters or keypresses: Ctrl-right_arrow, ctrl- left_arrow and then ctrl+shift-right_arrow.  (Insane, I know, but that's Microsoft word for you.)  Then s:=widestring(wordapp.Selection.Text);  will work. So, how do I send these actions to Word? I don't recall off the top of my head the virtual key codes for those key combinations but, search for "virtual key codes" and also WM_CHAR (that's the message you'll be sending to Word) HTH. ---------------------------------------------------------------------- w click · 2022-06-11 10:32 I don't understand the syntax for sendmessage, I'm afraid and there's a bewildering array of stuff on-line that doesn't make sense.  I tried combinations around this. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i  try    sendmessage(wordapp,VK_Lcontrol+VK_Right,0,1);    sendmessage(wordapp,VK_Lcontrol+VK_Left,0,1);    sendmessage(wordapp,VK_Lcontrol+VK_Lshift+VK_Right,0,1);  except    memo2.lines.add('Sendmessages failed');  end;  s:=widestring(wordapp.Selection.Text);  showmessage('<'+s+'>'); But the sendmessage fails, of course.  I know I'm doing it wrong, but all the searches give me stuff that makes no sense, is written in Russian or is my own questions! ---------------------------------------------------------------------- 440bx · 2022-06-11 11:04 that you don't understand the syntax of SendMessage is a problem because for what you want to do, you need to understand that. You should be sending Word, a WM_KEYUP for each key release you want Word to see.  Basically, your SendMessage should look something like this (for every character you want Word to see): Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i SendMessage(wordapp,     { presuming that's the correct value of Word's window handle },            WM_KEYUP,    { or possibly WM_KEYUP }            VK_RIGHT,    { character code of the key - quite likely or-ed with the shift you want },             0);          { sending 0 should work } Normally, it might take a little fiddling to feed the target program what it wants for it to do what you want it to do.  Since I don't have Word, I can only give a general direction but, nothing specific that I _know_ would work with it.  Feeding an app keys is something I very rarely need to do and, when I need to do it, I figure it out on the fly and the specifics don't stick in my mind. Hopefully, someone who's got Word and has more experience feeding keys to an app can provide further help. PS: now I remember, there is a unit that implements a "SendKeys" (or something along those lines) that could make it easier for you. ---------------------------------------------------------------------- w click · 2022-06-11 11:20 Sorry, I'm still getting '...raised exception class 'EVariantError' with message: invalid variant type cast' with that and various permutations tried. ---------------------------------------------------------------------- 440bx · 2022-06-11 14:42 Without an understanding of the Windows API, it's going to be difficult for you to get it working. Here is a post in stackoverflow that shows how to send keys to a program.  Read all the posts. https://stackoverflow.com/questions/15853732/sending-keystroke-to- another-application-using-winapi You'll need to adapt it for what you want and, get ready to do a lot of reading and trying things out before you get it to work. I suggest you start learning the process with a simpler program like notepad.  Once you've figured out how to successfully feed keys to notepad then, move on to Word. Also, I mentioned in my previous post the availability of SendKeys which would probably be easier for you but, it seems the library that implements it has some licencing requirements that not everyone is willing to meet. HTH. ---------------------------------------------------------------------- Thaddy · 2022-06-11 14:47 Quote from: 440bx on June 11, 2022, 04:42:06 pm but, it seems the library that implements it has some licencing requirements that not everyone is willing to meet. In that case someone stole intelectual property rights from someone who gave it away for nothing and with no license at all. See e.g. the D2 distribution. What I am aware of is that some version of sendkeys32 is at fault here. Your cold case team. ---------------------------------------------------------------------- w click · 2022-06-12 12:05 I went down that thread and did a few searches around, and followed the process. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.Button1Click(Sender: TObject);var notepad,edit : HWND;begin  if (sender as tbutton).caption='Button1' then begin    notepad := FindWindow('notepad', nil);    edit := FindWindowEx(notepad, FindWindow('Edit', nil), nil, nil);  end else begin    notepad := FindWindow('WINWORD', nil);     edit := FindWindowEx(notepad, FindWindow('Edit', nil), nil, nil);  end;  SendMessage(edit, WM_CHAR, dword('H'), 0);  SendMessage(edit, WM_CHAR, dword('e'), 0);  SendMessage(edit, WM_CHAR, dword('y'), 0);end;     Button1 for notepad works.  Button2 for Word does nothing, but with no error.  Using the code in my original procedure for the version of Word that the program calls up (i.e. SendMessage(wordapp, WM_CHAR, dword('H'), 0) creates the same exception class 'EVariantError' with message Invalid variant type cast'. ---------------------------------------------------------------------- 440bx · 2022-06-12 14:35 Quote from: w click on June 12, 2022, 02:05:00 pmI went down that thread and did a few searches around, and followed the process. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.Button1Click(Sender: TObject);var notepad,edit : HWND;begin  if (sender as tbutton).caption='Button1' then begin    notepad := FindWindow('notepad', nil);    edit := FindWindowEx(notepad, FindWindow('Edit', nil), nil, nil);  end else begin    notepad := FindWindow('WINWORD', nil);     edit := FindWindowEx(notepad, FindWindow('Edit', nil), nil, nil);  end;  SendMessage(edit, WM_CHAR, dword('H'), 0);  SendMessage(edit, WM_CHAR, dword('e'), 0);  SendMessage(edit, WM_CHAR, dword('y'), 0);end;     Button1 for notepad works.  Button2 for Word does nothing, but with no error.  Have you verified that Word's window class name is "Edit" ?  if Word uses a different class name the handle returned will be null which would definitely prevent the SendMessage call to work. Also, in general, you're calling FindWindow and FindWindowEx and not checking they executed successfully.  It's important to check for errors, otherwise, you're programming blind.  Do it one step at a time, check the result of every API call you make, that will give you clues about why it isn't working (when a call fails, call GetLastError to get an idea of why it failed.)  This is particularly important when you're trying to learn to do something. Quote from: w click on June 12, 2022, 02:05:00 pmUsing the code in my original procedure for the version of Word that the program calls up (i.e. SendMessage(wordapp, WM_CHAR, dword('H'), 0) creates the same exception class 'EVariantError' with message Invalid variant type cast'. There shouldn't be any variant records involved in feeding keys to a program.  If you have a variant record involved in the process: get rid of it!! ---------------------------------------------------------------------- w click · 2022-06-12 16:23 440bx, thank you for your help and persistence. The edit is a HWND. I went back to wordapp.selection when I found a moveright.  A word macro that does it is: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   wordapp.Selection.MoveRight Unit:=wdWord, Count:=1  wordapp.Selection.MoveLeft Unit:=wdWord, Count:=1  wordapp.Selection.MoveRight Unit:=wdWord, Count:=1, Extend:=wdExtend  So I tried lots of things, delving into website translated from Korean and German (I think), and this one actually worked: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   try    wordapp.Selection.MoveRight(wdword,1,0);    wordapp.Selection.MoveLeft(wdword,1,0);    wordapp.Selection.MoveRight(wdword,1,1);  except    memo2.lines.add('Sendmessages failed');  end;  s:=widestring(wordapp.Selection.Text);  showmessage('<'+s+'>'); I'll have a lot of tidying to do and got some dodgy error messages when shutting down, but I might even get this to work. I can't believe there isn't useful documentation on this. ---------------------------------------------------------------------- 440bx · 2022-06-12 16:52 Quote from: w click on June 12, 2022, 06:23:56 pm440bx, thank you for your help and persistence. So I tried lots of things, delving into website translated from Korean and German (I think), and this one actually worked: I'll have a lot of tidying to do and got some dodgy error messages when shutting down, but I might even get this to work. I can't believe there isn't useful documentation on this. You're welcome and I'm very pleased you found something that works for you.  There is documentation (in MSDN) but, the documentation assumes you have reasonably good knowledge of the Windows API.  Without that knowledge, it doesn't even look like documentation. ---------------------------------------------------------------------- w click · 2022-06-13 08:24 QuoteWithout that knowledge, it doesn't even look like documentation. That is so true. I don't feel I can mark this as solved.  It needs some readable documentation or a method to turn Word macros into pascal. Thanks again.  I'm sure I'll be back once I want to add something to the functionality. ---------------------------------------------------------------------- Thaddy · 2022-06-13 12:53 See https://github.com/frones/ACBr/blob/master/Projetos/ACBrMonitorPLU S/Lazarus/sndkey32.pas for the original. The license is just the copyright claim. 100% compatible with FreePascal's licensing.