This study investigates the adsorption of hydrogen cyanide (HCN) molecules onto both pure and transition metal (TM)-modified graphitic carbon nitride (g-C3N4) using density functional theory (DFT) and first-principles calculations in the solid state. The findings demonstrate that HCN adsorption induces structural changes in both unmodified and TM-doped g-C3N4 surfaces. Specifically, the initially flat structures of these materials transform into curved configurations following interaction with HCN gas. This structural modification correlates with a significant enhancement in electrical conductivity and improvements in electronic properties. Analysis of the partial density of states for TM-doped g-C3N4 and the orbitals of adsorbed HCN molecules reveals that the presence of transition metals and HCN adsorption increase electron density near the Fermi level. The calculated adsorption energies (Eads) for HCN on various surfaces are as follows: -0.281 eV on pure g-C3N4, -2.213 eV on Cr-doped, -2.326 eV on Mo-doped, and -3.104 eV on W-doped g-C3N4. On the pristine surface, HCN exhibits weak physical interaction, whereas the bonding becomes considerably stronger with the introduction of transition metals. The adsorption energy values suggest that HCN is physically adsorbed on the pure g-C3N4 surface, as the value exceeds -1 eV. Conversely, on TM-modified surfaces, the adsorption indicates chemical bonding, with energy values less than -1 eV. Among the modified systems, W-doped g-C3N4 exhibits the strongest interaction with HCN, with the lowest Eads value of -3.104 eV, surpassing the Cr- and Mo-doped variants. This characteristic makes W-doped g-C3N4 a promising candidate for detecting and capturing HCN molecules from environmental sources.