Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
342 views
in Technique[技术] by (71.8m points)

c# - Is there a way to disable CTRL + I, tab insertion in .NET UWP WebView

I am currently developing a UWP application and I have a WebView control, which a user interacts with. In this WebView, the user can format some text, and I would like for CTRL + I to apply italics formatting to any selected text.

When pressing CTRL + I the application formats the text (Which is handled using jQuery on the visited site), however, any selected text is replaced with a tab character.

I am aware that the CTRL + I keypress event results in keycode 9, which corresponds to a tab character, however, I am wondering if there is a way to disable this interaction?

I have tried intercepting both KeyDown and PreviewKeyDown events, however with no success. The events never fired, and I am assuming this is because I am entering text inside a webview. I also checked that the character is not inserted, when opening the visited page, using a normal web browser.

Below is the context of the webview.

<Page
    <!-- imports -->
    mc:Ignorable="d"
    Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

    <Grid>
        <WebView x:Name="Editor"
                 Source="ms-appx-web:///WebView.html"
                 ScriptNotify="Editor_ScriptNotify"
                 NavigationStarting="Editor_NavigationStarting"
                 NavigationCompleted="Editor_NavigationCompleted"/>
    </Grid>
</Page>
question from:https://stackoverflow.com/questions/65854885/is-there-a-way-to-disable-ctrl-i-tab-insertion-in-net-uwp-webview

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Is there a way to disable CTRL + I, tab insertion in .NET UWP WebView

I'm afraid you can't disable Ctrl + i from uwp side. the better way is process multiple key press with javascript in html side.

For example

 <body>
    <script type="text/javascript">

        let keysPressed = {};
        document.addEventListener('keyup', (event) => {
            delete keysPressed[event.key];
        });
        document.addEventListener('keydown', (event) => {
            keysPressed[event.key] = true;

            if (keysPressed['Control'] && event.key == 'i') {

                event.preventDefault();

                document.execCommand('italic', false, null);

            }
        });

    </script>
    <div id="editor1" contenteditable="true">
        The quick brown fox jumped over the lazy dog.
    </div>
</body>

You could detect keydown event and disable the default behavior with preventDefault method and add the execute new command when Ctrl + i pressed like above.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...