📜 ⬆️ ⬇️

My bike or how I saved my nerve cells

Greetings habrovchane!

For half a year now, as my main Desktop has been working on Ubuntu, I’m not going to write about the pros and cons of Linux, the post is not about that.
So ... unfortunately I don’t own a blind typing method, and there was no point in learning because in windows I was tolerably saved by the PuntoSwitcher program I used for about 5 years, but its analogue Xneur on Ubuntu, to put it mildly, worked "not very" and did not work at all in Skype.

For a while I put up with this, then tried unsuccessfully to write a small program in Java.
')
Requirements for the program:

  1. Quick start
  2. Tray icon to exit the application and cancel the conversion
  3. Global keyboard shortcut conversion


- this is the functionality that I used in the PuntoSwitcher.

image


And literally that night, I decided to go a different way and use sh.

I do not pretend to the universality of the method, but it works as it should on Kubuntu 15.10.

Principle of operation:

  1. On the sh label of the script hangs the key combination
  2. After launch using xclip we get the selected text
  3. Convert it to the opposite layout.
  4. Use the xdotool utility to insert the new text instead of the selected one.
  5. Change the layout to the opposite to continue typing.


From words to deeds:
#!/bin/bash oldtemp=`xclip -o` #  temp=`'/home/username/desktop/switch' "$oldtemp"` #  xdotool keydown Shift+Control_L #  sleep 1 xdotool keyup Shift+Control_L xdotool type "$temp" #   notify-send "$oldtemp -> $temp" -t 2000 #      () exit 0 


The xdotool keydown / xdotool keyup used instead of the xdotool key because the last one worked for me every other time.

Now about text conversion.
For this, I decided to use a separate program written in C ++ that would accept the text, understand in which direction it needs to be converted and would give the normal text to the console.

Prior to that, it was not necessary to write in C ++, the benefit of the functionality of this utility at the “Hello world” level.

Pregnant children with a weak psyche do not look
 #include <iostream> using namespace std; std::string Toreplace(std::string text, std::string s, std::string d){ for(int index=0; index=text.find(s, index), index!=std::string::npos;) { text.replace(index, s.length(), d); index+=d.length(); } return text; } int GetCurentLang(std::string str){ int thislang = 0; const char *cstr = str.c_str(); std::string russtr[66] = {"","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","",""}; std::string engstr[56] = {"q","w","e","r","t","y","u","i","o","p","[","]","a","s","d","f","g","h","j","k","l","z","x","c","v","b","n","m","<",">","Q","W","E","R","T","Y","U","I","O","P","A","S","D","F","G","H","J","K","L","Z","X","C","V","B","N","M"}; for (int i = 0; i < str.length() && thislang == 0; i++){ for (int a = 0; a < 66; a++){ if (russtr[a].compare(new char (cstr[i])) == true) { thislang = 1; } } for (int a = 0; a < 56; a++){ if (engstr[a].compare(new char (cstr[i])) == true) { thislang = 2; } } } return thislang; } std::string TranslateToRu(std::string str){ std::string translate = str; std::string replacein[67] = {"q","w","e","r","t","y","u","i","o","p","[","]","a","s","d","f","g","h","j","k","l",";","'","z","x","c","v","b","n","m",",",".","/","Q","W","E","R","T","Y","U","I","O","P","[","]","A","S","D","F","G","H","J","K","L",";","'","Z","X","C","V","B","N","M",",",".","/","&"}; std::string replaceto[67] = {"","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","",".","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","",".","?"}; for (int i = 0; i < 67; i++){ translate = Toreplace(translate, replacein[i], replaceto[i]); } return translate; } std::string TranslateToEng(std::string str){ std::string translate = str; std::string replacein[67] = {"","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","",".","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","",".","?"}; std::string replaceto[67] = {"q","w","e","r","t","y","u","i","o","p","[","]","a","s","d","f","g","h","j","k","l",";","'","z","x","c","v","b","n","m",",",".","/","Q","W","E","R","T","Y","U","I","O","P","[","]","A","S","D","F","G","H","J","K","L",";","'","Z","X","C","V","B","N","M","<",">","/","&"}; for (int i = 0; i < 67; i++){ translate = Toreplace(translate, replacein[i], replaceto[i]); } return translate; } int main(int argc, char *argv[]) { std::string translate; int currentlang = GetCurentLang(argv[1]); if (currentlang == 2){ translate = TranslateToRu(argv[1]); }else if (currentlang == 1){ translate = TranslateToEng(argv[1]); }else{ translate = ""; } cout << translate << endl; return 0; } 



GetCurentLang Accepts all the text entirely and searches for the first character in the Russian or English layout, if the first character we encounter in the English layout means that everything must be translated into Russian (ignore the characters, of course)

TranslateToEng , TranslateToRu an array of letters for each direction of yours to further customize the replacement more flexibly.

why std :: string and not char [] - as I said with C ++ is not familiar, and the Cyrillic alphabet does not fit in char [] ... (or something like that)

While the conversion goes in one direction, that is, if you write:
=djn.


at the output we get:

 <div id='main'>djn</div> 


it would be advisable to convert the “djn” too, but then the question will arise with the incorrect definition of the characters ",. /", etc.
In addition, the text selected via Ctrl + A does not fit into xclip, probably you should use instead:

 xdotool keyup Control_L+ 

Source: https://habr.com/ru/post/279499/


All Articles