如何获得选择从活动的应用程序的文本,而无需使用剪贴板 [英] How to get selected text from an active application, without using a clipboard

查看:180
本文介绍了如何获得选择从活动的应用程序的文本,而无需使用剪贴板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的主要目的是使用户友好文本到语音自用在Win 7方针应在谷歌Chrome,VS和Eclipse工作。

My main intent is to enable user friendly text to speech for personal use on Win 7. Approach should work in Google Chrome, VS and Eclipse.

以下代码为<大骨节病> CTRL全球键盘钩子 + <大骨节病> ALT + <大骨节病> 空格 ,堪称hookEvent。如果事件触发,它启动/停止说话剪贴板上的内容(可与<大骨节病> CTRL + <大骨节病> C是更新

Following code creates global keyboard hook for ctrl + alt + space, called hookEvent. If event fires, it starts/stops speaking clipboard contents ( that can be updated with ctrl + c ).

    /// <summary>
    /// KeyboardHook from: http://www.liensberger.it/web/blog/?p=207
    /// </summary>
    private readonly KeyboardHook hook = new KeyboardHook();
    private readonly SpeechSynthesizer speaker = //
        new SpeechSynthesizer { Rate = 3, Volume = 100 };

    private void doSpeaking(string text)
    {
        // starts / stops speaking, while not blocking UI
        if (speaker.State != SynthesizerState.Speaking)
            speaker.SpeakAsync(text);
        else
            speaker.SpeakAsyncCancelAll();
    }
    private void hookEvent(object sender, KeyPressedEventArgs e)
    {
        this.doSpeaking(Convert.ToString(Clipboard.GetText()));
    }
    public Form1()
    {
        InitializeComponent();
        hook.KeyPressed += new EventHandler<KeyPressedEventArgs>(hookEvent);
        hook.RegisterHotKey(ModifierKeysx.Control|ModifierKeysx.Alt, Keys.Space);
    }



问:



我宁愿不使用剪贴板。或者至少,恢复后的价值,是这样的:

Question:

I would prefer not using the clipboard. Or at least, restoring the value after, something like:

    [MethodImpl(MethodImplOptions.Synchronized)]
    private string getSelectedTextHACK()
    {
        object restorePoint = Clipboard.GetData(DataFormats.UnicodeText);
        SendKeys.SendWait("^c");
        string result = Convert.ToString(Clipboard.GetText());
        Clipboard.SetData(DataFormats.UnicodeText, restorePoint);

        return result;
    }



我有哪些选择?

What are my options?

编辑:

要我吃惊的是,我发现我的剪贴板读者是最好的一段路要走。我创建了一个通知区域的应用程序,响应左击(说剪贴板),然后右键单击(菜单打开)。在菜单中,用户可以有机会速度。说话或创建一个音频文件

To my surprise, I found that my clipboard reader is the best way to go. I created a notification area app, that responds to left click (speaking clipboard) and right click (menu opens up). In menu the user can chance speed, speak or create a audio file.

推荐答案

MS提供辅助工具,也包括你重新尝试做的。如果你看一看关于屏幕抓取的文件。总之,每个组件都是以某种方式访问​​,如果你使用一些窗口调试工具,你可以在这里看到内部的组件名称/结构。然后,您可以使用,但是,其作为最次复杂,你需要为每个你打算从凑应用非常具体的。

MS provide accessibility tools that do cover what you're trying to do. If you take a look at documents about screen scraping. In short, every component is accessible in some manner, if you use some of the windows debugging tools you can get to see the component names/structures within. You can then use that, however, its complicated as most times you would need to be very specific for each application you intend to scrape from.

如果你成功地刮你不需要使用剪贴板,你可以直接访问应用程序中的文本属性。它不是我不得不这样做,因此,我已经没有代码提供了我的头顶,但屏幕刮应该指向你在正确的方向。

If you manage to scrape you dont need to use the clipboard, as you can access the text property of the apps direct. Its not something I've had to do, hence, Ive no code to offer off the top of my head, but the term "screen scraping" should point you in the right direction.

这篇关于如何获得选择从活动的应用程序的文本,而无需使用剪贴板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆