![]() |
KnowBrainer Speech Recognition | ![]() |
Topic Title: How to automatically insert a space if necessary before executing a Sendkeys? Topic Summary: Created On: 02/04/2021 02:59 PM Status: Post and Reply |
|
![]() |
![]() |
- wristofdoom | - 02/04/2021 02:59 PM |
![]() |
![]() |
- Edgar | - 02/04/2021 03:53 PM |
![]() |
![]() |
- Lunis Orcutt | - 02/04/2021 08:46 PM |
![]() |
![]() |
- wristofdoom | - 02/05/2021 10:33 AM |
![]() |
![]() |
- Alan Cantor | - 02/04/2021 11:32 PM |
![]() |
![]() |
- wristofdoom | - 02/05/2021 10:21 AM |
![]() |
![]() |
- Alan Cantor | - 02/05/2021 12:21 PM |
![]() |
![]() |
- wristofdoom | - 02/05/2021 02:28 PM |
![]() |
![]() |
- monkey8 | - 02/05/2021 02:32 PM |
![]() |
![]() |
- Alan Cantor | - 02/05/2021 05:35 PM |
![]() |
![]() |
- wristofdoom | - 02/05/2021 05:43 PM |
![]() |
![]() |
- speechpro | - 02/06/2021 12:28 AM |
![]() |
![]() |
- PG LTU | - 02/06/2021 01:23 AM |
![]() |
![]() |
- wristofdoom | - 02/06/2021 10:46 AM |
![]() |
![]() |
- Edgar | - 02/06/2021 12:29 PM |
![]() |
![]() |
- wristofdoom | - 02/06/2021 01:08 PM |
![]() |
![]() |
- kkkwj | - 02/06/2021 03:33 AM |
![]() |
![]() |
- wristofdoom | - 02/06/2021 10:48 AM |
![]() |
![]() |
- PG LTU | - 02/06/2021 04:47 PM |
![]() |
|
I have a lot of commands that format text in a special way before sending it.
Very basic example: I have a command called "caps lock <dictation>" which will capitalize the text that I dictate.
>> SendKeys UCase ListVar1
For an example of the problem, imagine if I had this text:
"My text|"
The "|" character represents my cursor.
Notice that there is NOT a space after the word "text". In this case, if I were to dictate the command "caps lock my new text here", then the resultant text would be:
"My textMY NEW TEXT HERE|"
Not what we want! What I would like to do is add some code before the "SendKeys" line that will detect if there is a space prior to the cursor, and then add a space if there is not.
For example, imagine if I had that same text:
"My text|"
With my dream command, if I were to dictate the command "caps lock my new text here", then the command would look and see that there is no space before the cursor, so it would insert a space, and the resultant text would be:
"My text MY NEW TEXT HERE"
If there are existing commands out there that do this, I would love to take a look at the code so I can steal it.
I think what I am looking for is code that will select the character to the left of the cursor, detect if it is a space or not, and if it is not a space, then insert a space.
Thanks. ------------------------- Dragon Professional Individual v15.6. Windows 10. Knowbrainer 2017. |
|
|
|
![]() |
|
If there are existing commands out there that do this, I would love to take a look at the code so I can steal it.
I think what I am looking for is code that will select the character to the left of the cursor, detect if it is a space or not, and if it is not a space, then insert a space. Both of your assumptions are correct. I'm not going to bother to go searching for the code now as the author will probably chime in <grin>. But, if not, search (probably for "leading space") - it should be easy to find from the title! In your code start out by doing a:
------------------------- -Edgar |
|
|
|
![]() |
|
We are not certain why you need to duplicate existing KnowBrainer commands which are Title <dictation>, UPPER <dictation> and lower <dictation> ------------------------- Forum Mission Statement |
|
|
|
![]() |
|
Those Knowbrainer commands have the same issue that I've described.
Here's a video showing the "title <dictation>" command having the same problem I am trying to resolve here:
------------------------- Dragon Professional Individual v15.6. Windows 10. Knowbrainer 2017. |
|
|
|
![]() |
|
I think what I am looking for is code that will select the character to the left of the cursor, detect if it is a space or not, and if it is not a space, then insert a space. If you are dictating into Microsoft Word only, you can check the previous character without copying it to the clipboard. Selection.MoveLeft Unit:=wdCharacter, Extend:=wdExtend ' Select one character to the left If Selection = " " Then MsgBox "Yes, there is a space before the insertion point" Else MsgBox "No space before" End If Selection.MoveRight Unit:=wdCharacter ' Restore the cursor position It's not bulletproof. This script might fail if the insertion point is in a table, at the very start of a document, etc. But it should run at lightning speed and won't tie up the clipboard.
To use this code sample, you'll need to set a "Reference" to the "Microsoft Word xx.0 Object Library." The values of "xx" vary from version to version of Word. |
|
|
|
![]() |
|
Thank you. I use Microsoft Word a lot, so it would be helpful to have commands running in there even if it's not running everywhere else. ------------------------- Dragon Professional Individual v15.6. Windows 10. Knowbrainer 2017. |
|
|
|
![]() |
|
Once I find the version number from my version of Word (it's the most recent Office 365 version), would I put this reference? While focus is in the scripting pane, press Alt + Enter. This will open the "References" dialog box. Scroll down until you find the Microsoft Word xx.0 Object Library - it's a long way down! - and check the checkbox. You don't need to know the number. There is only one item in the list with this name, and I think it refers to the version of Word installed on your computer. Make sure you don't inadvertently choose the Microsoft Office xx.0 Object Library. It doesn't work here. |
|
|
|
![]() |
|
Thanks, this is great. This raises a question that I have always had with these scripts: how do I make a "does not equal" type condition?
In every coding language of ever used, != Is used to evaluate whether something is NOT a certain value. That doesn't work in the Dragon scripting language.
Currently what I have working correctly is: Sub Main Selection.MoveLeft Unit:=wdCharacter, Extend:=wdExtend ' Select one character to the left If Selection = " " Then But that obviously is weird and funky. I don't want to have an empty "then" case. What I want is something like this:
Sub Main Selection.MoveLeft Unit:=wdCharacter, Extend:=wdExtend ' Select one character to the left If Selection != " " Then SendKeys "{Space}" Selection.MoveRight Unit:=wdCharacter ' Restore the cursor position SendKeys UCase ListVar1 End Sub
but that doesn't work. Dragon code doesn't like the !=
I'm assuming there must be some sort of equivalent? ------------------------- Dragon Professional Individual v15.6. Windows 10. Knowbrainer 2017. |
|
|
|
![]() |
|
At the start of your script you can test for a space and insert a space if one does not exist using the following code which is the same as Edgard's pseudocode, this will work with any editor:
Sub Main Wait 0.1 SendKeys "^c" Wait 0.1 If(Clipboard = " ") Then SendKeys "{Right}" Else SendKeys "{Right} " End If … 'The rest of your script goes here …
EDIT: If (A <> B) for does not equal ------------------------- |
|
|
|
![]() |
|
If you're going to make this only for Word, consider upping its usefulness by, for example, checking if the previous character is a non-breaking space, a Tab character, a line break, and so on.
|
|
|
|
![]() |
|
Thanks everybody. I want a universal command first and foremost, but I also have a Microsoft Word specific alternative based on Alan's comment. ------------------------- Dragon Professional Individual v15.6. Windows 10. Knowbrainer 2017. |
|
|
|
![]() |
|
If you are referencing the word object, you do *not* have to move the cursor or use ^c to copy - you can test the character to the left of the cursor directly and act accordingly
... testText = ActiveDocument.Range(Start:=Selection.Start - 1, End:=Selection.Start) If testText <> " " Then sendkeys " " End If sendkeys "your text here" ...
|
|
|
|
![]() |
|
I did something like that. It still works. In many ways it's just like the above solutions, just adding more options. -------------------------
|
|
|
|
![]() |
|
I've got a lot to learn from PG's function, quoted below:
------------------------- Dragon Professional Individual v15.6. Windows 10. Knowbrainer 2017. |
|
|
|
![]() |
|
I hope the formatting remains intact… Sub Main ' do some stuff DoSomething ' do some more stuff DoSomething' do the exact same thing over again End Sub Function DoSomething() ' do something special and complicated ' involving many lines of code End Function "clipboard" & Clipboard$() - just two ways of referring to the same thing. The second way might be more historical but either works just as well. ------------------------- -Edgar |
|
|
|
![]() |
|
Amazing, okay. I didn't know this was possible, although I wanted to do something like this. I am constantly reusing code snippets in my scripts, and I wish I could just call a function so that I only need update the snippets in one place, rather than going to all my scripts that use the same snippet.
------------------------- Dragon Professional Individual v15.6. Windows 10. Knowbrainer 2017. |
|
|
|
![]() |
|
Lots of times, I want to join utterances *without* a space, which is a situation where a "universal" command that forces in a space would not do the right thing. I just made separate commands to add a space (or not), add a leading capital (or not), add title case, and so on.
------------------------- Win10/x64, AMD Ryzen 7 3700X, 64GB RAM, Dragon 15.3, SP 6 PRO, SpeechStart, Office 365, KB 2017, Dragon Capture, Samson Meteor USB Desk Mic, Klim and JUKSTG earbuds with microphones, 3 BenQ 2560x1440 monitors, Microsoft Sculpt Keyboard and fat mouse |
|
|
|
![]() |
|
I run into that situation myself, but I have the opposite problem more often than needing to join text together. ------------------------- Dragon Professional Individual v15.6. Windows 10. Knowbrainer 2017. |
|
|
|
![]() |
|
The function can be used anywhere once defined. Yes, you can access it in a '#Uses referenced link, or just put it in the same command (after the End Sub line). Then use it. It will put a space or two where and when needed or do nothing when it is not needed.
Also note, the forum changes two spaces to one, so the code for after a period and other terminal punctuation needs a second space if you use two spaces after sentences. Also, the Clipboard thing is just from much older versions of Dragon when I first wrote this. -------------------------
|
|
|
FuseTalk Standard Edition v4.0 - © 1999-2021 FuseTalk™ Inc. All rights reserved.