Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM forum
Suggested answer

Is keypress event works in Dynamics 365

Posted on by 2
OnLoad Event, I writen the below code.  iam getting error Cannot read properties of null (reading 'addOnKeyPress')
 
function alertLead(executionContext)
{
    alert(/Welcome2/);
   var formContext = executionContext.getFormContext();
   alert(Xrm.Page.getAttribute(/budgetamount/).getValue())
    var nameKeyPressFunction = function () {
           var key = event.keyCode;
           alert(key);
       }
       Xrm.Page.getControl(/budgetamount/).addOnKeyPress(nameKeyPressFunction);
}
 
 
  • Suggested answer
    Pradeep Rai Profile Picture
    Pradeep Rai 5,490 Super User 2024 Season 2 on at
    Is keypress event works in Dynamics 365
    To create and Build PCF controls we need to perform multiple steps:
     
    1. You need to run this command to setup the PCF Control:
    pac pcf init --namespace <namespace> --name <controlname> --template field --run-npm-install
     
    2. After execution multiple files will get created and you need to use index.ts file. Try adding below code:
     
     
    import { IInputs, IOutputs } from "./generated/ManifestTypes";
    
    export class CustomTextboxControl implements ComponentFramework.StandardControl<IInputs, IOutputs> {
    
        private _inputElement: HTMLInputElement;
    
        constructor(private context: ComponentFramework.Context<IInputs>, private notifyOutputChanged: () => void, private container: HTMLDivElement) {
            // Create and append input element to the container
            this._inputElement = document.createElement("input");
            this._inputElement.setAttribute("type", "text");
            this.container.appendChild(this._inputElement);
    
            // Add keypress event listener
            this._inputElement.addEventListener('keypress', this.handleKeyPress.bind(this));
        }
    
        public init(): void {
            // Initialize your control
        }
    
        public updateView(context: ComponentFramework.Context<IInputs>): void {
            // Update your control view
        }
    
        public getOutputs(): IOutputs {
            // Return outputs
            return {};
        }
    
        public destroy(): void {
            // Cleanup code
            this._inputElement.removeEventListener('keypress', this.handleKeyPress.bind(this));
        }
    
        private handleKeyPress(event: KeyboardEvent): void {
          
            alert("test");
        }
    }
     
     
    3. Once done you need upload this control dynamic CRM for this please refer below link:
    https://learn.microsoft.com/en-us/power-apps/developer/component-framework/import-custom-controls
     
    You can follow below link to create the PCF Controls:
  • Suggested answer
    Dengliang Li Profile Picture
    Dengliang Li Microsoft Employee on at
    Is keypress event works in Dynamics 365
    Hi,
     
    You can get the HTML element for a specific input field. Then add the KeyPress event to this element.
    The code is shown below.
     
    function onload(){
        // Gets the specific input field HTML element.
        let first_name = window.top.document.querySelector("input[aria-label='First Name']");
     
        //Determines if this HTML element is null.
        if(first_name){
           
            //Adds the KeyPress event to the input text field.
            first_name.addEventListener("keypress",(e)=>{
     
                alert(e.code)
            })
        }else{
     
        //If null, refetch every 10 milliseconds until not null.
            setTimeout(onload,10);
        }
    }
     
    When I press the A key in the input text field, it displays a dialog box with the value of the key code.
     
     
    If my answer was helpful, please click Like, and if it solved your problem, please mark it as verified to help other community members find more.
    If you have further questions, please feel free to contact me .
     
    Best Regards,
    Dengliang Li
     
  • Is keypress event works in Dynamics 365
     Could you please give me any example of Power Apps component framework using Keypress Event for Lead Module in the Sales Trail.  one the Account Name field should be use Alphanumeric not special character. 
  • Suggested answer
    Adrian Begovich Profile Picture
    Adrian Begovich 21,009 Super User 2024 Season 2 on at
    Is keypress event works in Dynamics 365
    Hi,
     
    addOnKeypress is a Deprecated Client API. The recommended replacement is a custom control created using Power Apps component framework.
     
    Deprecated Client API Replacement Client API
    addOnKeyPress Use a custom control created using Power Apps component framework
    fireOnKeyPress Use a custom control created using Power Apps component framework
    removeOnKeyPress Use a custom control created using Power Apps component framework

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Anton Venter – Community Spotlight

Kudos to our October Community Star of the month!

Announcing Our 2024 Season 2 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Dynamics 365 Community Newsletter - September 2024

Check out the latest community news

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 290,532 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 228,501 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,148

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans