Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Blazor] @onkeydown:preventDefault not working on Android browsers #42836

Closed
1 task done
daniel-p-tech opened this issue Jul 20, 2022 · 11 comments
Closed
1 task done

[Blazor] @onkeydown:preventDefault not working on Android browsers #42836

daniel-p-tech opened this issue Jul 20, 2022 · 11 comments
Assignees
Labels
area-blazor Includes: Blazor, Razor Components External This is an issue in a component not contained in this repository. It is open for tracking purposes.
Milestone

Comments

@daniel-p-tech
Copy link

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

I created a fairly complex mask component that relies heavily on @onkeydown:preventDefault="m_preventDefaultKeyDown" logic to suppress the user from entering invalid characters etc. This works great in all desktop browsers and when emulating mobile devices using developer tools, but when running the same (WASM) app on mobile browsers (Chrome and Firefox on Android), I am able to enter any characters as if preventDefault was not set to true.

I can easily reproduce this issue using a very simple input element:

<input type="text" class="form-control"
    @onkeydown:preventDefault
    @onchange:preventDefault />

I tried adding preventDefault and stopPropagation flags to all onxyz events just in case with no success.

What is going on? This seems to be a major bug in Blazor and I suspect by now all 3rd party component vendors would have issues with this, so maybe I'm doing something incorrectly?

Thank you.

Expected Behavior

Explained above.

Steps To Reproduce

<input type="text" class="form-control"
    @onkeydown:preventDefault
    @onchange:preventDefault />

Exceptions (if any)

No response

.NET Version

6.0.107

Anything else?

Tested on Chrome 103.0.5060.71 running on Android 10; SM-G960U

@Pilchie Pilchie added the area-blazor Includes: Blazor, Razor Components label Jul 20, 2022
@daniel-p-tech
Copy link
Author

Out of curiosity I ran a demo of DevExpress MaskedInput component on Chrome (Android) and was able to immediately break it. I suspect they use Blazor server and I know nothing further about the implementation details of their component but it's possibly related to the same problem that I'm running into with my own component.

https://demos.devexpress.com/blazor/MaskedInput

@mkArtakMSFT mkArtakMSFT added this to the 7.0-rc2 milestone Jul 21, 2022
@daniel-p-tech
Copy link
Author

@mkArtakMSFT I see that this issue has been assigned to a milestone - can you explain what this means? Is this a confirmed bug that will be fixed in https://github.com/dotnet/aspnetcore/milestone/218?

@daniel-p-tech
Copy link
Author

Hi, is there any update on this? It has been two weeks without any feedback.

@SteveSandersonMS
Copy link
Member

@daniel-p-tech Is it possible you're just running into an Android browser limitation? I see there are StackOverflow posts about this exact kind of thing, not involving Blazor: https://stackoverflow.com/questions/42011740/event-preventdefault-not-working-for-android-chrome. It might be that you need to preventDefault on the input event instead of keydown.

I did try the following repro: https://blazorrepl.telerik.com/mGEjubvk53gTKnFt09. On iOS Safari it behaves as expected, preventing characters from being typed.

@SteveSandersonMS SteveSandersonMS added the Needs: Author Feedback The author of this issue needs to respond in order for us to continue investigating this issue. label Sep 1, 2022
@ghost
Copy link

ghost commented Sep 1, 2022

Hi @daniel-p-tech. We have added the "Needs: Author Feedback" label to this issue, which indicates that we have an open question for you before we can take further action. This issue will be closed automatically in 7 days if we do not hear back from you by then - please feel free to re-open it if you come back to this issue after that time.

@ghost
Copy link

ghost commented Sep 5, 2022

This issue has been automatically marked as stale because it has been marked as requiring author feedback but has not had any activity for 4 days. It will be closed if no further activity occurs within 3 days of this comment. If it is closed, feel free to comment when you are able to provide the additional information and we will re-investigate.

See our Issue Management Policies for more information.

@daniel-p-tech
Copy link
Author

I don't see how I can rewrite my code to use oninput since I need to be able to acquire the actual key:

// good reference: https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key/Key_Values
if (args.Key == "ArrowLeft" || args.Key == "ArrowRight" ||
    args.Key == "ArrowDown" || args.Key == "ArrowUp" ||
    args.Key == "Home" || args.Key == "End")
{
    // enable default browser behaviour
    m_preventDefaultKeyDown = false;
}
else if (args.Key == "Delete")
{
}
else if (args.Key == "Backspace")
{
...
}
else
{
...
}

Also, the demo that you linked to does not work on Android mobile - I was able to enter everything I typed.

Thank you.

@ghost ghost added Needs: Attention 👋 This issue needs the attention of a contributor, typically because the OP has provided an update. and removed Needs: Author Feedback The author of this issue needs to respond in order for us to continue investigating this issue. Status: No Recent Activity labels Sep 5, 2022
@SteveSandersonMS
Copy link
Member

SteveSandersonMS commented Sep 6, 2022

@daniel-p-tech As far as I can tell this is just an Android browser limitation. I tried https://jsfiddle.net/4thnxmjw/ (which doesn't involve Blazor at all) and on PC browsers it prevents all keys except a (as designed), whereas on Chrome for Android it doesn't prevent any keys - I was able to type anything.

If you have any reason to think this is a Blazor issue, please provide details.

@SteveSandersonMS SteveSandersonMS added Needs: Author Feedback The author of this issue needs to respond in order for us to continue investigating this issue. and removed Needs: Attention 👋 This issue needs the attention of a contributor, typically because the OP has provided an update. labels Sep 6, 2022
@ghost
Copy link

ghost commented Sep 6, 2022

Hi @daniel-p-tech. We have added the "Needs: Author Feedback" label to this issue, which indicates that we have an open question for you before we can take further action. This issue will be closed automatically in 7 days if we do not hear back from you by then - please feel free to re-open it if you come back to this issue after that time.

@SteveSandersonMS SteveSandersonMS changed the title [Blazor] @onkeydown:preventDefault not working on mobile browsers [Blazor] @onkeydown:preventDefault not working on Android browsers Sep 6, 2022
@daniel-p-tech
Copy link
Author

Do you have any suggestions on how to implement a proper mask component that would work on all browsers? As I previously mentioned, oninput event does not provide the necessary key code. I put two weeks worth of work into this. Maybe one of the interns can look into implementing a true cross-browser mask component (which virtually every app requires) and share it with the world now that you have a basic grid component?

Thanks!

@SteveSandersonMS
Copy link
Member

I'm afraid I don't have a specific suggestion but since this isn't a Blazor-specific limitation, it should be possible to look at any JavaScript component that does what you want and follow the same approach.

Closing this as an external issue - sorry we didn't have an easy solution for you!

@SteveSandersonMS SteveSandersonMS closed this as not planned Won't fix, can't repro, duplicate, stale Sep 7, 2022
@SteveSandersonMS SteveSandersonMS added External This is an issue in a component not contained in this repository. It is open for tracking purposes. and removed Needs: Author Feedback The author of this issue needs to respond in order for us to continue investigating this issue. investigate labels Sep 7, 2022
@dotnet dotnet locked as resolved and limited conversation to collaborators Oct 7, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-blazor Includes: Blazor, Razor Components External This is an issue in a component not contained in this repository. It is open for tracking purposes.
Projects
None yet
Development

No branches or pull requests

4 participants