コメントで紹介した記事を試してみました。Windows10 64bit, Python 3.7.6 です。
keyboard 0.13.4とpywin32 227のModule win32guiを使っています。
対象ウインドウへフォーカスはこちらの回答を使用
Python Window Activation
キー押下の通知はこちらの質問を使用
Python “keyboard” module: Release keys to allow copy/Ctrl+C
受信側のスクリプトは何も変更していません。
通知側のスクリプトは以下。CTRL+Cの通知
import win32gui
import re
import keyboard
class WindowMgr:
"""Encapsulates some calls to the winapi for window management"""
def __init__ (self):
"""Constructor"""
self._handle = None
def find_window(self, class_name, window_name=None):
"""find a window by its class_name"""
self._handle = win32gui.FindWindow(class_name, window_name)
def _window_enum_callback(self, hwnd, wildcard):
"""Pass to win32gui.EnumWindows() to check all the opened windows"""
if re.match(wildcard, str(win32gui.GetWindowText(hwnd))) is not None:
self._handle = hwnd
def find_window_wildcard(self, wildcard):
"""find a window whose title matches the wildcard regex"""
self._handle = None
win32gui.EnumWindows(self._window_enum_callback, wildcard)
def set_foreground(self):
"""put the window in the foreground"""
win32gui.SetForegroundWindow(self._handle)
w = WindowMgr()
w.find_window_wildcard(".*receiver_test.*") # 検索するウインドウタイトルはスクリプト名のもの
w.set_foreground()
keyboard.press_and_release('ctrl+c')
CTRL+BREAKは上記の最後を以下に変更
keyboard.press_and_release('ctrl+break')
結果は以下の様に、ctrl+c
を通知するとSIGINT
の処理が行われてRECEIVED!
の表示で処理が続行され、ctrl+break
を通知すると^C
を表示してスクリプトが終了しています。

これが使えるか/使いやすいか、は分かりませんが、こんなことも出来るということで。