Skip to main content
Microsoft Dynamics CRM forum
Suggested answer

Is keypress event works in Dynamics 365

editSubscribe (0) ShareShare
ReportReport
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,483 Super User 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 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 20,302 Super User 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

Helpful resources

Quick Links

What Motivates a Super User?

We know many of you visit the Dynamics 365 Community and Power Platform…

Demystifying Copilot with Georg Glantschnig…

Industry experts answer burning questions directly from our amazing Community…

Setting Up Knowledge Sources for Copilot…

Look at how configuring a comprehensive knowledge base is crucial…

Leaderboard

#1
Andre Arnaud de Calavon Profile Picture

Andre Arnaud de Cal... 283,045 Super User

#2
Martin Dráb Profile Picture

Martin Dráb 222,570 Super User

#3
nmaenpaa Profile Picture

nmaenpaa 101,138

Product updates

Dynamics 365 release plans